CreateThread Function
The CreateThread function creates a new thread to execute within the virtual address space of the calling process.
Syntax
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);
Parameters
| Parameter | Description |
|---|---|
lpThreadAttributes |
A pointer to a If |
dwStackSize |
The initial size, in bytes, of the stack for the new thread. If this parameter is 0, the function uses the default size for the stack of the calling process. The default stack size is 1 megabyte. For more information, see Stack Sizes. |
lpStartAddress |
A pointer to the application-defined function of type
|
lpParameter |
A pointer to a variable to be passed to the thread function. |
dwCreationFlags |
The flags that control the thread's creation. This parameter can be one of the following values:
|
lpThreadId |
A pointer to a 32-bit value that receives the thread identifier of the new thread. If this parameter is NULL, the thread identifier is not returned. |
Return Value
If the function succeeds, the return value is a handle to the new thread. Each thread has a unique handle that can be used with various functions.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
A thread in a process shares the same address space as the process. A thread object is created and runs within the context of the process.
The thread function (specified by lpStartAddress) should be of type LPTHREAD_START_ROUTINE. This function receives a single parameter, lpParameter, and should return a 32-bit value. This return value is the exit code of the thread. The calling thread can use the GetExitCodeThread function to retrieve this value.
The handle returned by CreateThread can be used to specify the thread in any function that requires a thread handle, such as the synchronization functions. The handle is created with full access rights to the new thread.
To close a thread handle, call CloseHandle. It is not necessary to close the handle when a thread terminates.
If you are creating a thread for a process that is being debugged, the debugger can use the DEBUG_ONLY_THIS_PROCESS flag to specify that the thread should only be created when the process is being debugged.
Requirements
| Component | Value |
|---|---|
| Minimum supported client | Windows XP [desktop apps only] |
| Minimum supported server | Windows Server 2003 [desktop apps only] |
| Target Platform | Windows |
| Header | threadpoolapis.h (include windows.h) |
| Library | User32.lib |
| DLL | User32.dll |