Will the thread terminate even if it is in suspended state when TerminateThread
is called?
1 Answers
The TerminateThread function destroys the thread regardless of its state or the likely side effects. The linked MSDN page covers this in some detail.
TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code. DLLs attached to the thread are not notified that the thread is terminating. The system frees the thread's initial stack.
Windows Server 2003 and Windows XP: The target thread's initial stack is not freed, causing a resource leak.
TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:
I've got to ask why you would want to call this as it's definitely a last resort for shutting down a thread. your application will leak memory and other resources unless you are very lucky or careful.

- 1
- 1

- 13,575
- 1
- 42
- 75
-
@ Ade Miller thanks for the info. I have now modified the code by synchronizing the thread during shutdown using WaitForSingleObject in destructor. – CodeRider Mar 05 '14 at 08:32
-
You should consider the ramifications of waiting in the destructor. http://stackoverflow.com/questions/6710129/c-concurrency-and-destructors. A better strategy would be to have an explicit shutdown method. – Ade Miller Mar 05 '14 at 14:57