6

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.

x-x
  • 7,287
  • 9
  • 51
  • 78
  • By recursive do you mean that it has a counter (like the reference counter in smart pointers)? – sashoalm Oct 29 '12 at 07:59
  • 5
    Take a look at this: http://stackoverflow.com/questions/1988324/how-to-alter-the-recursive-locking-behaviour-of-windows-mutex – Alex F Oct 29 '12 at 08:08
  • @AlexFarber, A Mutex is a different thing from a critical section. – x-x Oct 29 '12 at 09:16
  • The point is that if you don't want a recursive lock, you need to use a mutex – David Heffernan Oct 29 '12 at 09:34
  • 1
    Not clear what your goal is here; are you trying to detect attempted recursive use to flag it as (potential) error, for example? You could add your own code to do this for critsects that your own code owns, by pairing each one with a 'in use' bool; or for debug-only code, you could [dig into the critical section internals](http://msdn.microsoft.com/en-us/magazine/cc164040.aspx) to check its state. – BrendanMcK Oct 29 '12 at 09:56
  • I see that you want only exact answer, at it is: no. – Alex F Oct 29 '12 at 11:19

2 Answers2

8

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

zneak
  • 134,922
  • 42
  • 253
  • 328
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
4

A Critical Section always allows recursion within a thread. So does a Mutex. That is by design and cannot be changed.

A Semaphore, on the other hand, can prevent recursion within a thread. See MSDN documentation for more details.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770