Ram
Ram

Reputation: 1497

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?

Upvotes: 2

Views: 1299

Answers (2)

goblinjuice
goblinjuice

Reputation: 3214

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

Upvotes: 4

Olof Forshell
Olof Forshell

Reputation: 3274

You will have to define "dies."

Do you mean that it faults through an incorrect memory or other access? Then the entire process is thrown out by the OS.

Do you mean that the thread is terminated (either by exiting or by some other thread terminating it)? Then you're in trouble because everything has executed correctly including the thread termination so all threads waiting on the critical section will be stuck forever.

Do you mean that the thread enters an endless loop? I'm not sure how Windows handles such a situation but there are two strategies: either the OS will assume "everything appears to be running correctly so I should stay away" or there will be a system-wide CPU quota which is used up causing the OS to terminate the process. Only the process ( a thread in it) can cause a single thread to terminate, all other mechanisms will throw out the entire process.

Upvotes: 1

Related Questions