Scheduling Attributes
This section describes the Win32-specific support, behavior, and restrictions for thread scheduling attributes.
On this page:
Start Policy
The start policy attribute is fully supported by the Win32 implementation of the Threads Module and defaults to RW_THR_START_RUNNING.
Scheduling Contention Scope
In Win32, all threads are scheduled by the kernel and are have a fixed contention scope value of RW_THR_SYSTEM_SCOPE.
You may query the contention scope, and may set the contention scope to RW_THR_SYSTEM_SCOPE, but any attempt to change the scope to RW_THR_PROCESS_SCOPE will produce an exception.
Scheduling Inheritance Policy
The scheduling inheritance policy attribute is fully supported by the Win32 implementation of the Threads Module and defaults to RW_THR_INHERIT.
Concurrency Policy
The concurrency policy attribute is not supported in the Win32 implementation of the Threads Module. Attempts to get or set this attribute value will result in exceptions.
Scheduling Policy
In Win32, the scheduling policy of a thread is related to the priority class of the thread’s process. Since the process is assigned to a priority class, all threads within a process will have the same scheduling policy. To change the scheduling policy used, you would have to change the priority class of the process. Changing the priority class will affect all threads within the process.
The Threads Module does not provide any mechanism for changing the priority class of a process; you can do this directly by using the SetPriorityClass() function in the Win32 API. You should use care when doing this; running a process in the HIGH_PRIORITY_CLASS or REALTIME_PRIORITY_CLASS may interfere with normal operation of the operating system.
The Threads Module maps the Win32 priority classes to RWSchedulingPolicy values as follows:
|
Win32 Priority Class |
Threads Module Scheduling Policy |
|
|
|
|
|
|
|
|
|
|
|
|
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.
Each Win32 priority class defines a different base priority value for threads created in that class; see Scheduling Priority for additional information.
You may query for the default scheduling policy, and may set the scheduling policy to match the current scheduling policy, but any attempt to change the policy will produce an RWTHROperationNotAvailable exception.
The default scheduling policy is determined by querying the API for the priority-class of the current process and converting that result to a scheduling policy according to the mapping defined in the previous table.
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:
|
Threads Module Priority Values |
Win32 Priority Values |
|
-3 |
|
|
-2 |
|
|
-1 |
|
|
0 |
|
|
+1 |
|
|
+2 |
|
|
+3 |
|
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.)
|
|
Threads Module Priority Levels |
||||||
|
Win32 Process Priority Class |
-3 |
-2 |
-1 |
0 |
+1 |
+2 |
+3 |
|
|
1 |
2 |
3 |
4 |
5 |
6 |
15 |
|
|
1 |
5 |
6 |
7 |
8 |
9 |
15 |
|
|
1 |
7 |
8 |
9 |
10 |
11 |
15 |
|
|
1 |
11 |
12 |
13 |
14 |
15 |
15 |
|
|
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.
Scheduling Time-Slice Quantum
The time-slice quantum attribute is not supported in the Win32 implementation of the Threads Module. Attempts to get or set this attribute value will result in exceptions.