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?
2 回答
来自 MSDN:
如果线程在拥有临界区所有权时终止,则临界区的状态未定义。
来源: http: //msdn.microsoft.com/en-us/library/windows/desktop/ms682608 (v=vs.85).aspx
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.