Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function ends a thread.

Syntax

VOID ExitThread(
  DWORD 
dwExitCode
);

Parameters

dwExitCode

[in] Specifies the exit code for the calling thread. To retrieve a thread's exit code, use the GetExitCodeThreadfunction.

Return Value

None.

Remarks

The preferred method of exiting from a thread is to return from the thread procedure so the system implicitly calls ExitThread, and C++ function cleanup occurs.

When ExitThreadis called (explicitly or by returning from a thread procedure), the current thread's stack is deallocated and the thread terminates.

The entry-point function of all attached DLLs is invoked with a value indicating that the thread is detaching from the DLL.

If the thread is the main thread in the process when this function is called, the thread's process is also terminated.

If the primary thread calls the ExitThreadfunction, the application exits.

The state of the thread object becomes signaled, releasing other threads waiting for the thread to terminate.

The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCodeparameter.

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

The ExitThreadand CreateThreadfunctions, as well as a process that starts as the result of a CreateProcesscall, are serialized between each other within a process. Only one of these events can happen in an address space at a time.

The following list shows the restrictions during the process:

  • During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.

  • In a process, only one thread at a time can be in a DLL initialization or detach routine.

Requirements

Header winbase.h
Library coredll.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also