3 out of 10 rated this helpful - Rate this topic

TerminateThread function

Applies to: desktop apps only

Terminates a thread.

Syntax

BOOL WINAPI TerminateThread(
  __inout  HANDLE hThread,
  __in     DWORD dwExitCode
);

Parameters

hThread [in, out]

A handle to the thread to be terminated.

The handle must have the THREAD_TERMINATE access right. For more information, see Thread Security and Access Rights.

dwExitCode [in]

The exit code for the thread. Use the GetExitCodeThread function to retrieve a thread's exit value.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

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:

  • 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.

If the target thread is the last thread of a process when this function is called, the thread's process is also terminated.

The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter.

Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

CreateProcess
CreateThread
ExitThread
GetExitCodeThread
OpenThread
Process and Thread Functions
Terminating a Thread
Threads

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Exception 0xC000071C=STATUS_INVALID_THREAD while calling TerminateThread

This exception is thrown, when you try to TerminateThread a Thread owned by the threadpool. I'd expect TerminateThread to not throw. This behaviour is definitely a bug. TerminateThread should just return 0 in case an error.

One excellent reason not to use it

TerminateThread is able to kill threads that are currently holding critical sections deep within the Windows API, such as in the sockets layer. If a thread is killed while holding a critical section, that section stays locked forever, and your program can grind mysteriously to a halt. This can be a real pain to diagnose, particularly when it’s in the bowels of a third party package. (cURL, for instance, does this as a way to time out threads waiting for DNS, unless you build it with c-ares.)

To be used with precaution
This function should be used when all the possibilities to signal to a thread that "it's time to die" are exhausted. In the real life, if you are coding properly, you will never execute this function. And yet, I am still coding the "request thread end" function with a TerminateThread on the tail, just in case ...
Don't use it. Don't

The warnings above are a gross understatement. TerminateThread is NOT a solution for shutting down worker threads. You will break your app, and it will fail randomly.

Set a flag for the thread to watch for and then voluntarily shut itself down. If your threads are hanging, fix the hang. Involuntarily killing threads only makes things worse.