Process and Thread Priority and Scheduling
The Windows kernel always runs the highest-priority thread that is ready for execution. A thread is not ready if it is waiting, suspended, or blocked for some reason.Threads receive priority relative to their process priority classes. Four process priority classes are set initially by CreateProcess, as described in Chapter 6, and each has a base priority.
- IDLE_PRIORITY_CLASS, base priority 4
- NORMAL_PRIORITY_CLASS, base priority 9 or 7
- HIGH_PRIORITY_CLASS, base priority 13
- REALTIME_PRIORITY_CLASS, base priority 24
The two extreme classes are rarely used, and the normal class can be used most of the time. Windows NT (all versions) is not a real-time OS, but CE is, and REALTIME_PRIORITY_CLASS should be used with care so as not to prevent other processes from running. The normal base priority is 9 if the window has the focus for keyboard input; otherwise, the priority is 7.A process can change or determine its own priority or that of another process, security permitting.
BOOL SetPriorityClass (HANDLE hProcess,
DWORD dwPriority)
DWORD GetPriorityClass (HANDLE hProcess)
Thread priorities are set relative to the process base priority, and, at thread creation time, the priority is set to that of the process. The thread priorities are in a range of ±2 from the process's base. The symbolic names of the resulting five thread priorities are as follows:
- ThrEAD_PRIORITY_LOWEST
- THREAD_PRIORITY_BELOW_NORMAL
- ThrEAD_PRIORITY_NORMAL
- ThrEAD_PRIORITY_ABOVE_NORMAL
- ThrEAD_PRIORITY_HIGHEST
Use these values to set and read a thread's relative priority. Note the use of signed integers rather than DWORDs.
BOOL SetThreadPriority (HANDLE hThread,
int nPriority)
int GetThreadPriority (HANDLE hThread)
There are actually two additional thread priority values. They are absolute rather than relative and are used only in special cases.
- ThrEAD_PRIORITY_IDLE is a value of 1 (or 16 for real-time processes).
- THREAD_PRIORITY_TIME_CRITICAL is 15 (or 31 for real-time processes).
Thread priorities change automatically with process priority. In addition, the OS may adjust thread priorities dynamically on the basis of thread behavior. You can enable and disable this feature with the SetThreadPriorityBoost function.
Thread and Process Priority Cautions
High thread priorities and process priority classes should be used with caution. Real-time priorities should definitely be avoided for normal user processes; real-time priorities should be used only if the application is truly real-time. Among other dangers, user threads may preempt threads in the executive.Furthermore, everything that we say in the following chapters about the correctness of threaded programs assumes, without comment, that thread scheduling is fair. Fairness ensures that all threads will, eventually, run. Without fairness, a low-priority thread could hold resources required by a high-priority thread. Thread starvation and priority inversion are terms used to describe the defects that occur when scheduling is not fair.
