ExitThread function
Namespace: Windows API
Header: processthreadsapi.h
Syntax
#include <processthreadsapi.h>
VOID WINAPI ExitThread(
_In_ DWORD dwExitCode
);
Parameters
dwExitCode | The exit code for the thread. It is available to the thread's creator via GetExitCodeThread. |
Return value
This function does not return. The thread terminates and the exit code is made available to the thread’s creator.
Remarks
- Calling
ExitThreadterminates the current thread and does not unwind the stack of other threads. - All thread local storage (TLS) callbacks are executed before the thread exits.
- Do not call
ExitThreadfrom a DLL entry-point function; useFreeLibraryAndExitThreadinstead. - If the thread was created with
CreateThread, the exit code can be retrieved by the creating thread usingGetExitCodeThread.
Example
#include <windows.h>
#include <stdio.h>
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
printf("Thread %d starting.\n", GetCurrentThreadId());
// Perform work...
ExitThread(0);
// Never reached
return 0;
}
int main(void)
{
HANDLE hThread = CreateThread(
NULL, 0, ThreadProc, NULL, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
DWORD dwExitCode;
GetExitCodeThread(hThread, &dwExitCode);
printf("Thread exited with code %lu\\n", dwExitCode);
CloseHandle(hThread);
return 0;
}