Win32 API Threads
The Win32 API provides a comprehensive set of functions for creating and managing threads, enabling concurrent execution within a process. Threads are the smallest unit of execution that an operating system can schedule.
Core Thread Management Functions
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
Creates a new thread to execute within the virtual address space of the calling process.
Parameters:
lpThreadAttributes: Pointer to a SECURITY_ATTRIBUTES structure that determines the security attributes of the new thread.
dwStackSize: The initial size, in bytes, of the stack for the new thread.
lpStartAddress: Pointer to the application-defined function to be executed by the new thread.
lpParameter: The argument to be passed to the thread function.
dwCreationFlags: Flags that control the thread's creation.
lpThreadId: Pointer to a 32-bit variable that receives the thread identifier.
Returns a handle to the newly created thread.
VOID ExitThread(DWORD dwExitCode)
Terminates the calling thread and provides an exit code.
Parameters:
dwExitCode: The exit code for the thread.
This function does not return a value.
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
Waits until the specified object is in the signaled state or the time-out interval elapses. Often used to wait for a thread to complete.
Parameters:
hHandle: A handle to the object.
dwMilliseconds: The time-out interval in milliseconds.
Returns a value indicating why the wait was terminated.
HANDLE OpenThread(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId)
Obtains a handle to an existing thread.
Parameters:
dwDesiredAccess: The access to the thread object.
bInheritHandle: If this parameter is TRUE, then the handle inherited by the new process has the same value and access rights as the handle in the parent process.
dwThreadId: The identifier of the thread to be opened.
Returns a handle to the specified thread.
DWORD GetCurrentThreadId(VOID)
Retrieves the thread identifier of the calling thread.
Returns the thread identifier of the calling thread.
Thread Synchronization
Effective multithreaded programming requires mechanisms to coordinate thread execution and prevent race conditions.
Synchronization Primitives:
- Mutexes (Mutual Exclusion Objects): Used to protect shared resources by ensuring that only one thread can access the resource at a time. Functions include
CreateMutex, OpenMutex, ReleaseMutex.
- Semaphores: Act as counters to control access to a pool of resources. Functions include
CreateSemaphore, ReleaseSemaphore.
- Events: Used to signal that a specific condition has occurred. Functions include
CreateEvent, SetEvent, ResetEvent.
- Critical Sections: A lightweight synchronization mechanism suitable for protecting shared data within a single process. Functions include
InitializeCriticalSection, EnterCriticalSection, LeaveCriticalSection.
Key Functions:
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
Waits for the specified objects to enter the signaled state.
Parameters:
nCount: The number of handles in the array.
lpHandles: An array of handles to the objects.
bWaitAll: If TRUE, the function returns when all objects in the array are signaled. If FALSE, the function returns when any one of the objects is signaled.
dwMilliseconds: The time-out interval.
Returns a value indicating why the wait was terminated.
Thread Information and Control
BOOL GetThreadPriorityBoostCount(HANDLE hThread, PDWORD pdwDisableBoost)
Retrieves the number of times a thread's priority has been boosted. (Note: This is a more advanced function, often used for performance tuning).
Returns TRUE if successful, FALSE otherwise.
BOOL SetThreadPriority(HANDLE hThread, int nPriority)
Sets the priority of the specified thread.
Parameters:
hThread: A handle to the thread whose priority is to be set.
nPriority: The priority value.
Returns TRUE if successful, FALSE otherwise.
Thread Local Storage (TLS)
TLS allows each thread in a process to have its own copy of a variable, ensuring that threads do not interfere with each other's data.
TlsAlloc(): Allocates a TLS index.
TlsGetValue(DWORD dwTlsIndex): Retrieves the value for the calling thread for the specified TLS index.
TlsSetValue(DWORD dwTlsIndex, LPVOID lpValue): Sets the thread-specific value for the specified TLS index.
TlsFree(DWORD dwTlsIndex): Frees a TLS index.