MSDN Documentation

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 to THREAD_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 ClassesREALTIME_PRIORITY_CLASS for critical tasks.

API Reference

FunctionHeaderDescription
SetThreadPriorityprocessthreadsapi.hSets the priority of a thread.
GetThreadPriorityprocessthreadsapi.hRetrieves the priority of a thread.
SetThreadAffinityMaskprocessthreadsapi.hSets the processor affinity mask for a thread.
CreateTimerQueueTimerthreadpoollegacyapiset.hCreates a timer in the timer queue.
RegisterWaitForSingleObjectthreadpoolapiset.hRegisters 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.