winapicritical-section

Can a Windows CRITICAL_SECTION object be configured to deny recursive access?


By default, a CRITICAL_SECTION object is recursive. Can this behaviour be configured like a pthread mutex to enable or disable recursive thread access?

To clarify in response to the comments: I am referring specifically to a Windows CRITICAL_SECTION object, not a Windows mutex.


Solution

  • No, it cannot. Documented APIs do not mention this in any way. Windows critical sections always accept recursive access.

    When a thread owns a critical section, it can make additional calls to EnterCriticalSection or TryEnterCriticalSection without blocking its execution. This prevents a thread from deadlocking itself while waiting for a critical section that it already owns.

    Source: https://learn.microsoft.com/en-us/windows/win32/sync/critical-section-objects