Click here to Skip to main content
Licence CPOL
First Posted 7 Dec 2008
Views 14,118
Bookmarked 4 times

A Safe Mutex for Deadlocks

By | 7 Dec 2008 | Article
mutex wrapper class

Introduction

we see simple thread synchronization with Win32 API and MFC.

Background

Mutex:

A mutex object is a synchronization object whose state is set to signaled when it is not owned by any thread, and nonsignaled when it is owned.Only one thread at a time can own a mutex object, whose name comes from the fact that it is useful in coordinating mutually exclusive access to a shared resource. For example, to prevent two threads from writing to shared memory at the same time, each thread waits for ownership of a mutex object before executing the code that accesses the memory. After writing to the shared memory, the thread releases the mutex object.

Example1:

deadlock 1

void CMyClass::DoSomething()
{
  HANDLE hMutex = ::CreateMutex( NULL, FALSE, lpszName );
  ::WaitForSingleObject(m_hMutex, INFINITE);
  if( i == 0) return; // < -- deadlock
  else printf("foo");
  ReleaseMutex(hMutex);
}

Example2:

deadlock 2

void CMyClass::DoSomething()
{
  EnterCriticalSection(&m_cs)
  if( i == 0) return; // < -- deadlock
  else printf("foo");
  LeaveCriticalSection(&m_cs); 
}

Using the code

avoid deadlock ( mutithread safe )

class CSimpleMutex {
public:
 CSimpleMutex(LPCTSTR lpszName = NULL) : m_hMutex(NULL) {
   m_hMutex = ::CreateMutex( NULL, FALSE, lpszName );
   ::WaitForSingleObject(m_hMutex, INFINITE);
 }
 virtual ~CSimpleMutex() {
   if (m_hMutex) {
     ReleaseMutex(m_hMutex);
   }
 }
protected:
 HANDLE m_hMutex;
}     

DoSomething0 and DoSomething1 are synchronized

void CMyClass::DoSomething0()
{
  CSimpleMutex m("lock1");
  if( foo == NULL) return;
  return;
}
void CMyClass::DoSomething1()
{
  CSimpleMutex m("lock1");
  if( foo == NULL) return;
  return;
}
void CMyClass::DoSomething2()
{
  CSimpleMutex m("another lock");
  if( foo == NULL) return;
  return;
}

Conclusion

CSimpleMutex is very simple, Happy programming!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

tommy500

Software Developer

Korea (Republic Of) Korea (Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmembertretererte5:37 24 Nov '09  
GeneralMy vote of 1 PinmemberCoolVini22:43 15 Sep '09  
GeneralMy vote of 1 PinmemberRam Jawahar Pandey18:56 11 Jan '09  
GeneralMy vote of 1 PinmemberWilliam E. Kempf8:53 9 Dec '08  
GeneralMy vote of 1 PinmemberJohn M. Drescher6:11 9 Dec '08  
GeneralMy vote of 1 PinmemberRick York14:49 8 Dec '08  
GeneralGood Idea! Pinmemberyun hye shin13:26 8 Dec '08  
GeneralMy vote of 1 PinmemberCPAV10:15 8 Dec '08  
GeneralMy vote of 1 Pinmembermacabre13@o2.pl9:03 8 Dec '08  
GeneralNice idea PinmemberBakaBug7:12 8 Dec '08  
GeneralMy vote of 1 PinmemberAndre xxxxxxx3:30 8 Dec '08  
GeneralMy vote of 1 Pinmemberbert_r2:19 8 Dec '08  
Questionthat all? Pinmemberczyzyx2:03 8 Dec '08  
GeneralMy vote of 1 PinmemberManish K. Agarwal0:50 8 Dec '08  
GeneralOMG! Pinmembermikhailitsky0:12 8 Dec '08  
GeneralMy vote of 1 PinPopularmemberMrARSoft19:43 7 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 8 Dec 2008
Article Copyright 2008 by tommy500
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid