Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C++
Alternative
Tip/Trick

Terminate a hanging thread

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Dec 2011CPOL1 min read 15.5K   1   5
The right thing to do in this case is attach a debugger to determine the underlying cause of the the deadlock. This solution ignores the fact that the thread being terminated might have an exclusive lock on other critical resources (e.g., the OS loader lock or any application defined...
The right thing to do in this case is attach a debugger to determine the underlying cause of the the deadlock.

This solution ignores the fact that the thread being terminated might have an exclusive lock on other critical resources (e.g., the OS loader lock or any application defined synchronization object). For this reason, terminating a thread can work by happy accident in a lot of cases, but in general terminating a thread is inherently unsafe on the Windows platform.

For example, I encountered a deadlock that occurred due to calling WinVerifyTrust() from DllMain(). An undocumented feature of WinVerifyTrust() is that it can spawn a worker thread, which requires the OS loader lock ... and the OS loader lock was already acquired by the OS loader before the call to DllMain(). That's a classic deadlock. If we terminated the thread, then the underlying call would simply fail, which would ignore the underlying problem (illegal call from DllMain).

I have also encountered deadlocks in interprocess RPC where the lock order of various synchronization objects was random between different processes and threads, which can result in more complex deadlocks. Again, terminating the thread would simply cause the RPC to fail, and not address the real problem (undefined lock order).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRtlFreeUserThreadStack is also available in Server 2003 (NT ... Pin
nicklowe4-Jan-12 16:35
nicklowe4-Jan-12 16:35 
GeneralThe return type for RtlFreeUserThreadStack is NTSTATUS and n... Pin
nicklowe4-Jan-12 14:28
nicklowe4-Jan-12 14:28 
GeneralThe return type for RtlFreeUserThreadStack is NTSTATUS and n... Pin
nicklowe4-Jan-12 14:01
nicklowe4-Jan-12 14:01 
GeneralI half-agree with your statements... fixing a deadlock is gr... Pin
Albert Holguin2-Dec-11 7:56
professionalAlbert Holguin2-Dec-11 7:56 
I half-agree with your statements... fixing a deadlock is great of course, but avoiding one all together, while still alerting of the problem, is even better than that. Having your code hangup in odd manners may be a good way to force a developer to fix it, but it will by no means please a customer.
GeneralHi John, this is not a recommendation to use TerminateThread... Pin
trotwa28-Nov-11 13:17
trotwa28-Nov-11 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.