INFO: Critical Sections Versus Mutexes (105678)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 3.1
    • the operating system: Microsoft Windows NT 3.5
    • the operating system: Microsoft Windows NT 3.51
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows XP

This article was previously published under Q105678

SUMMARY

Critical sections and mutexes provide synchronization that is very similar, except that critical sections can be used only by the threads of a single process. There are two areas to consider when choosing which method to use within a single process:
  1. Speed. The Synchronization overview says the following about critical sections:

    ... critical section objects provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization.

    Critical sections use a processor-specific test and set instruction to determine mutual exclusion.
  2. Deadlock. The Synchronization overview says the following about mutexes:

    If a thread terminates without releasing its ownership of a mutex object, the mutex is considered to be abandoned. A waiting thread can acquire ownership of an abandoned mutex, but the wait function's return value indicates that the mutex is abandoned.

    WaitForSingleObject() will return WAIT_ABANDONED for a mutex that has been abandoned. However, the resource that the mutex is protecting is left in an unknown state.

    There is no way to tell whether a critical section has been abandoned.

Modification Type:MinorLast Reviewed:3/7/2005
Keywords:kbinfo kbKernBase kbThread kbThreadSync KB105678