CreateThread Function
The CreateThread function creates a new thread to execute within the virtual address space of the calling process.
Syntax
HANDLE CreateThread(
[in, optional] LPSECURITY_ATTRIBUTES lpThreadAttributes,
[in, optional] SIZE_T dwStackSize,
[in] LPTHREAD_START_ROUTINE lpStartAddress,
[in, optional] LPVOID lpParameter,
[in, optional] DWORD dwCreationFlags,
[out, optional] LPDWORD lpThreadId
);
Parameters
-
lpThreadAttributes
A pointer to a SECURITY_ATTRIBUTES structure that specifies the security attributes of the new thread. If this parameter is NULL, the thread gets a default security descriptor that has access to the same जन्माष्टमी it has.
The size of the security descriptor is determined by the length of the DACL. If the DACL is NULL, the thread will have the same access जन्माष्टमी it has.
-
dwStackSize
The desired size of the new thread's stack, in bytes. If this parameter is zero, the system uses the default size for the executable. The actual stack size is rounded up to the nearest page. If the size is too small, the function fails.
-
lpStartAddress
A pointer to the application-defined function of type LPTHREAD_START_ROUTINE that the thread should begin executing at. This is a pointer to the function that the thread will execute. The function must take a single parameter of type LPVOID and return a DWORD.
-
lpParameter
A pointer to a variable to be passed to the new thread. The thread function can use this parameter to receive data. It must be cast to LPVOID.
-
dwCreationFlags
Flags that control the creation of the thread. This parameter can be zero or the following value:
- CREATE_SUSPENDED (0x00000004) The thread is created in a suspended state. Use ResumeThread to cause the thread to begin execution.
-
lpThreadId
A pointer to a 32-bit variable that receives the thread identifier. 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. The handle has maximum access to the new thread. If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
Each process is created with a single thread. Threads can create additional threads. Threads share the same address space, code, data, and operating system resources, such as files and synchronization objects.
The thread function typically uses the lpParameter to receive data from the creating thread. For example, a thread might pass a pointer to a structure containing data or a pointer to an array of strings.
The thread terminates if it calls the ExitThread function or if it returns from the thread function. The thread's exit status is the value returned by the thread function.
Example
The following example demonstrates how to create a new thread that executes a function named ThreadProc:
#include <windows.h>
#include <iostream>
// Thread function
DWORD WINAPI ThreadProc(LPVOID lpParam) {
int value = *((int*)lpParam);
std::cout << "Thread received value: " << value << std::endl;
return 0;
}
int main() {
HANDLE hThread;
DWORD threadID;
int data = 42;
// Create the thread
hThread = CreateThread(
NULL, // Default security attributes
0, // Default stack size
ThreadProc, // Thread function
&data, // Parameter to thread function
0, // Default creation flags
&threadID); // Returns the thread identifier
if (hThread == NULL) {
std::cerr << "Failed to create thread. Error: " << GetLastError() << std::endl;
return 1;
}
std::cout << "Thread created successfully with ID: " << threadID << std::endl;
// Wait for the thread to finish
WaitForSingleObject(hThread, INFINITE);
// Close the thread handle
CloseHandle(hThread);
return 0;
}