Windows Scheduling Overview
Windows scheduling controls how the operating system allocates CPU time to threads and processes. It provides mechanisms for priority management, real‑time execution, and background processing.
Concepts
API Reference
Sample Code
Key Concepts
- Thread Priority Levels – From
THREAD_PRIORITY_IDLE
toTHREAD_PRIORITY_TIME_CRITICAL
. - Processor Affinity – Restricts a thread to specific CPU cores.
- Quantum – The time slice a thread receives before preemption.
- Work‑Conserving Scheduler – Ensures cores are utilized efficiently.
- Real‑Time Classes –
REALTIME_PRIORITY_CLASS
for critical tasks.
API Reference
Function | Header | Description |
---|---|---|
SetThreadPriority | processthreadsapi.h | Sets the priority of a thread. |
GetThreadPriority | processthreadsapi.h | Retrieves the priority of a thread. |
SetThreadAffinityMask | processthreadsapi.h | Sets the processor affinity mask for a thread. |
CreateTimerQueueTimer | threadpoollegacyapiset.h | Creates a timer in the timer queue. |
RegisterWaitForSingleObject | threadpoolapiset.h | Registers a wait operation for a kernel object. |
Sample: Adjusting Thread Priority
// Compile with: cl /EHsc priority_demo.cpp #include <windows.h> #include <iostream> int main() { HANDLE hThread = GetCurrentThread(); if (!SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST)) { std::cerr << "Failed to set priority. Error: " << GetLastError() << std::endl; return 1; } std::cout << "Thread priority set to highest." << std::endl; // Simulate work Sleep(3000); return 0; }
This example raises the current thread's priority to THREAD_PRIORITY_HIGHEST
. Use with caution on production systems.