Search code examples
windowsmultithreadingvisual-c++critical-section

What will happen if a thread dies inside the critical section?


This is the scenario. We have lots of thread accessing a section of code that is protected by a critical section, which ensures that only one thread at a time will gain access to that part. Now the question is, what will happen if a thread dies inside the critical section? Will the application hang? or there is some way that lock will be released?


Solution

  • From MSDN:

    If a thread terminates while it has ownership of a critical section, the state of the critical section is undefined.

    Source: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608(v=vs.85).aspx