Scheduling Priority
As with scheduling policy, the true priority of a thread under Win32 is related to the priority class of the thread’s process. Win32 defines a range of priority values from 1 to 31 for use in scheduling threads. Each thread is assigned a base-priority within this range according to the priority class of the process and a priority offset defined by the thread.
The priority class of the process defines a base priority level for all threads in that process. Each thread possesses a priority level offset, that when combined with the base-priority of the priority-class, gives a thread its own base priority value.
The scheduler uses this base priority to determine the thread's dynamic priority, the value used to make scheduling decisions. A thread's dynamic priority is never less than its base priority.
The scheduler raises and lowers the dynamic priority of a thread to enhance its responsiveness when significant things happen to the thread, unless the thread is executing in a process with REALTIME_PRIORITY_CLASS; threads in this priority class will not have their priority dynamically altered.
Dynamic priority changes, or boosts can occur in the following situations:
*A window receives input (such as timer messages, mouse move messages, keyboard input).
*The parent process owns a window that has become active.
*A wait for disk or keyboard I/O is satisfied.
After raising a thread's dynamic priority, the scheduler reduces that priority by one level each time the thread completes a time slice, until the thread drops back to its base priority.
In addition to these dynamic priority boosts, the scheduler raises the priority class of the process associated with the foreground window, so it is greater than or equal to the priority class of any background processes. The process's priority class returns to its original setting when it is no longer in the foreground.
Dynamic boosts are also used to break the deadlock that can result from priority inversion. The Windows scheduler does this by randomly boosting the priority of threads that are ready to run (in this case the low priority lock-holders). The low priority threads run long enough to let go of their lock (exit the critical section), and the high- priority thread gets the lock back. If the low-priority thread doesn't get enough CPU time to free its lock the first time, it will get another chance on the next scheduling round.
Since threads operating in the real-time priority class do not receive dynamic boosts, care must be taken to insure that these threads do not deadlock as the result of priority inversion.
This table shows the legal range of the Threads Module priority values for Win32, and the mapping between those priority values and the Win32 priority values:
Table 13 – Win32: Mapping of Threads Module priority values to Win32 values 
Threads Module Priority Values
Win32 Priority Values
-3
THREAD_PRIORITY_IDLE
-2
THREAD_PRIORITY_LOWEST (-2)
-1
THREAD_PRIORITY_BELOW_NORMAL (-1)
0
THREAD_PRIORITY_NORMAL
+1
THREAD_PRIORITY_ABOVE_NORMAL (+1)
+2
THREAD_PRIORITY_HIGHEST (+2)
+3
THREAD_PRIORITY_TIME_CRITICAL
The following table shows how the Threads Module priority values map to the true priority levels used by the operating system. This mapping is directly related to the current priority class of the process. (The base priority of each priority class appears underlined.)
Table 14 – Win32: How the current Win32 priority class determines true priority values
 
Threads Module Priority Levels
Win32 Process Priority Class
-3
-2
-1
0
+1
+2
+3
IDLE_PRIORITY_CLASS
1
2
3
4
5
6
15
NORMAL_PRIORITY_CLASS(Background window)
1
5
6
7
8
9
15
NORMAL_PRIORITY_CLASS(Foreground window)
1
7
8
9
10
11
15
HIGH_PRIORITY_CLASS
1
11
12
13
14
15
15
REALTIME_PRIORITY_CLASS
16
22
23
24
25
26
31
The default priority class for a process is NORMAL_PRIORITY_CLASS, unless the priority class of the creating process is IDLE_PRIORITY_CLASS, in which case the default priority class of the child process is IDLE_PRIORITY_CLASS. This means that the typical default priority for a Win32 thread is either 7 or 9, depending on whether the process is associated with a foreground or background window. Threads with a base priority level above 11 can interfere with the normal operation of the operating system.