2.1.4.1.5 CreateThread


Description

Creates a thread to execute within the virtual address space of the calling process.

Syntax

HANDLE CreateThread( PSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId )

Parameters

lpThreadAttributes
[input] A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
dwStackSize
[input] The initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable.
lpStartAddress
[input] A pointer to the application-defined function to be executed by the thread and represents the starting address of the thread.
lpParameter
[input] A pointer to a variable to be passed to the thread.
dwCreationFlags
[input] The flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation.
lpThreadId
[output] A pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.


Return

If the function succeeds, the return value is a handle to the new thread.

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


Examples

Remark

The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2028 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.

See Also

Header to Include

origin.h

Reference