+ Reply to Thread
Results 1 to 2 of 2

Thread: What is Mutex?

  1. #1

    What is Mutex?

    Hi,

    What is Mutex? How it is different than critical section?

    Regards.

  2. #2
    Hi,

    Mutex is synchronization object. You can use it to synchronize access to shared variable.

    Critical section is also a synchronization object and can be useful to synchronize access to shared variable.

    Following is difference:

    Mutex:
    a. Mutex is a kernel object, it is slow than critical section.
    b. Can use WaitFor* APIs.
    c. Name mutex can work within the process boundaries.
    d. Mutex is in WAIT_ABANDONED state when owner of the thread terminates without releasing it.

    Critical Section:
    a. Critical section is user mode object, hence it is fast then Mutex.
    b. In Windows, one thread is allowed to lock critical section more than once.
    c. Critical section cannot use WaitFor* APIs.
    d. Critical section works only within the single application.

    Code:
    void UsingMutex()
    {
    	HANDLE hMutex = CreateMutex(NULL, TRUE, pszApplicationName) ;
    
    	if (NULL != hMutex)
    	{
                    ReleaseMutex(hMutex) ;
    		CloseHandle(hMutex) ;
    	}
    }
    Regards.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts