TerminateThread

TerminateThread terminates a thread.

Syntax

Copy
BOOL TerminateThread(
    [in, out]   HANDLE hThread,
    [in]        DWORD ExitCode
);

Parameters

[in, out] hThread

The thread to terminate.

[in] ExitCode

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

Return Value

If the function succeeds, it returns TRUE. If the function fails, it returns FALSE. Call GetLastError to get extended error information.

Remarks

TerminateThread causes a thread to exit. When this occurs, the target thread cannot execute any user-mode code.

Note: TerminateThread is a DANGEROUS function that should only be used in extreme cases.

Call TerminateThread only if you know exactly what the target thread is doing, and control all of the code that it could possibly be running at the time of the termination.

A thread cannot protect itself against TerminateThread, other than by controlling access to its handles.

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

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.

Under Windows, when a thread's existence ends by being the target of a TerminateThread call, the thread's stack is not automatically freed, as it may contain useful debugging information. RTSS follows this behavior, so when an RTSS user thread is the target of a TerminateThread call, the memory for that thread's stack will not be returned to the non-paged memory pool. We have provided a new wRTOS Settings option to modify this behavior to preserve memory. If you have wRTOS Runtime installed, and you enable Free Stack on Terminate Thread Calls in wRTOS Settings, RTSS will free the memory used for the stack when a thread is terminated via TerminateThread.

Requirements

Minimum supported version Header Library

wRTOS 1.0 SDK

windows.h

wRTOS_rtss.lib

See Also: