Click here to Skip to main content
Click here to Skip to main content

A Safe Mutex for Deadlocks

, 7 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

tommy500
Software Developer
Korea (Republic Of) Korea (Republic Of)
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembertretererte24-Nov-09 6:37 
GeneralMy vote of 1 PinmemberCoolVini15-Sep-09 23:43 
GeneralMy vote of 1 PinmemberRam Jawahar Pandey11-Jan-09 19:56 
GeneralMy vote of 1 PinmemberWilliam E. Kempf9-Dec-08 9:53 
GeneralMy vote of 1 PinmemberJohn M. Drescher9-Dec-08 7:11 
GeneralMy vote of 1 PinmemberRick York8-Dec-08 15:49 
GeneralGood Idea! Pinmemberyun hye shin8-Dec-08 14:26 
GeneralMy vote of 1 PinmemberCPAV8-Dec-08 11:15 
GeneralMy vote of 1 Pinmembermacabre13@o2.pl8-Dec-08 10:03 
GeneralNice idea PinmemberBakaBug8-Dec-08 8:12 
GeneralMy vote of 1 PinmemberAndre xxxxxxx8-Dec-08 4:30 
GeneralMy vote of 1 Pinmemberbert_r8-Dec-08 3:19 
Questionthat all? Pinmemberczyzyx8-Dec-08 3:03 
GeneralMy vote of 1 PinmemberManish K. Agarwal8-Dec-08 1:50 
GeneralOMG! Pinmembermikhailitsky8-Dec-08 1:12 
GeneralMy vote of 1 PinmemberMrARSoft7-Dec-08 20:43 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 8 Dec 2008
Article Copyright 2008 by tommy500
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid