Relative Priority
The other factor in determining the overall priority of a thread is the relative priority of a particular thread. The important distinction to make is that the priority class is associated with a process and the relative priority is associated with individual threads within a process. A thread can have any one of seven possible relative priorities:
- Idle
- Lowest
- Below Normal
- Normal
- Above Normal
- Highest
- Time Critical
TThread exposes a Priority property of an enumerated type TThreadPriority. There's an enumeration in this type for each relative priority:
type TThreadPriority = (tpIdle, tpLowest, tpLower, tpNormal, tpHigher, tpHighest, tpTimeCritical);
You can get and set the priority of any TThread object simply by reading from or writing to its Priority property. The following code sets the priority of a TThread descendant instance called MyThread to Highest:
MyThread.Priority := tpHighest.
Like priority classes, each relative priority is associated with a numeric value. The difference is that relative priority is a signed value that, when added to a process's class priority, is used to determine the overall priority of a thread within the system. For this reason, relative priority is sometimes called delta priority. The overall priority of a thread can be any value from 1 to 31 (1 being the lowest). Constants are defined in the Windows unit that represents the signed value for each priority. Table 2 shows how each enumeration in TThreadPriority maps to an API constant.
Table 2 Relative Priorities for Threads
TThreadPriority |
Constant |
Value |
tpIdle* |
THREAD_PRIORITY_IDLE |
-15 |
tpLowest |
THREAD_PRIORITY_LOWEST |
-2 |
tpBelow Normal |
THREAD_PRIORITY_BELOW_NORMAL |
-1 |
tpNormal |
THREAD_PRIORITY_NORMAL |
0 |
tpAbove Normal |
THREAD_PRIORITY_ABOVE_NORMAL |
1 |
tpHighest |
THREAD_PRIORITY_HIGHEST |
2 |
tpTimeCritical* |
THREAD_PRIORITY_TIME_CRITICAL |
15 |
The reason the values for the tpIdle and tpTimeCritical priorities are marked with asterisks is that, unlike the others, these relative priority values are not truly added to the class priority to determine overall thread priority. Any thread that has the tpIdle relative priority, regardless of its priority class, has an overall priority of 1. The exception to this rule is the Realtime priority class, which, when combined with the tpIdle relative priority, has an overall value of 16. Any thread that has a priority of tpTimeCritical, regardless of its priority class, has an overall priority of 15. The exception to this rule is the Realtime priority class, which, when combined with the tpTimeCritical relative priority, has an overall value of 31.