Critical Sections Versus Mutexes

Last reviewed: January 17, 1997
Article ID: Q105678
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5, 3.51, 4.0
        - Microsoft Windows 95 version 4.0
    

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.


KBCategory: kbprg
KBSubcategory: BseSync
Additional reference words: 3.50 3.51 4.00 95


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.