August 5th, 2004, 01:18 PM
Threads: How to end a thread?
Q: How to end a thread?
A: There are different ways depending whether the thread will be ended from inside or outside. For ending a thread from inside the function 'ExitThread()' can be used.
This will end the actual thread. It is the preferred method to end a thread. This function will also be called implicitly while returning from the thread procedure and provides a clean shutdown of the thread.
ExitThread(0); // Zero is the exit code in this example
To end a thread from outside there exist also two general possibilities. To provide a clean exit of the thread an event can be set which was passed as a thread parameter and which is frequently checked by the thread procedure.
If the approach described above is not possible for several reasons (e.g. a lengthy database query is performed) then the thread can be forced to exit by a call to 'TerminateThread()'.
static UINT ThreadFunction(LPVOID pvParam);
// Create events
m_StopThread = CreateEvent(0, TRUE, FALSE, 0);
m_WaitThread = CreateEvent(0, TRUE, FALSE, 0);
// Start thread
// Trigger thread to stop
// Wait until thread finished
// Close handles
UINT CFoo::ThreadFunction(LPVOID pvParam)
CFoo *pParent = static_cast<CFoo*>(pvParam);
// Check event for stop thread
if(::WaitForSingleObject(pParent->m_StopThread, 0) == WAIT_OBJECT_0)
// Set event
// Do your processing
'hThread' is the handle of the thread which can be obtained by use of any of the functions described in 'How to create a worker thread?'. On Windows NT, Windows 2000 and Windows XP systems, the handle must have 'THREAD_TERMINATE' access.
if(TerminateThread(hThread, 0) == FALSE)
// Could not force thread to exit -> call 'GetLastError()'
'TerminateThread()' has some drawbacks which should be considered. It causes the thread to exit. When this occurs, the thread does not have the chance to execute any user-mode code any longer. DLLs attached to the thread are not notified that the thread is terminating.
Additional information is provided by the MSDN:
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:
- If the target thread owns a critical section, the critical section will not be released.
- If the target thread is allocating memory from the heap, the heap lock will not be released.
- If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
- If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.
A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.
Last edited by cilu; October 26th, 2007 at 04:21 AM.
Reason: removed incorrect pointer
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!