Recently my friend Nibu has blogged about controlling process priority at run time. On seeing that I feel like to write on the basics of thread and process priorities. Of course veterans of Windows programming will not be able make anything better out of this post.
Each process in the system has assigned a certain level of priority when it’s created. Most of the programs are running in the system having normal priority. Some special programs like task manager will be having “Real-Time” priority level. This means, those programs are having higher chance to get the CPU allocated. There are different classes defined to specify the process priority.
| Priority class | Symbolic Identifier | Description | |
| Real-Time | REAL_TIME_PRIORITY_CLASS | Used to handle time critical tasks. Threads in this process can pre-empts operating system tasks. So carefully use this priority class. | |
| High | HIGH_PRIORITY_CLASS | High response to the events. E.g Task manager application | |
| Above Normal | ABOVE_NORMAL_PRIORITY_CLASS | Slightly above normal priority | |
| Below Normal | BELOW_NORMAL_PRIORITY_CLASS | Below normal priority | |
| Idle | IDLE_PRIORITY_CLASS | Threads in this process scheduled when the system is idle. Used by screensavers, indexing services etc. | |
The priority for a process can be specified when it’s created using CreateProcess API. If nothing specified, the process will run under normal priority.
It’s possible to modify the priority of a Process at run-time using SetPriorityClass API. The function takes following form
[sourcecode language='cpp']
BOOL WINAPI SetPriorityClass( __in HANDLE hProcess, __in DWORD dwPriorityClass );
[/sourcecode]
It’s possible to specify any process handle to modify the thread priority class of corresponding process, provided you have a valid handle and permission to modify the process priority. To modify the priority of your own process can be done in the following way
[sourcecode language='cpp']
SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS);
[/sourcecode]
You can get the current priority of a process by using GetPriorityClass function.
[sourcecode language='cpp']
DWORD WINAPI GetPriorityClass( __in HANDLE hProcess );
[/sourcecode]
OK that’s about Process Priority. Here comes about Thread Priority level. Process and thread priority levels are different and related. As specified above the threads are also having time-critical, high, above normal, below normal and idle priorities. The symbolic names are namely THREAD_PRIORITY_TIME_CRITICAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_IDLE
You can specify a thread’s priority by calling SetThreadPriority Function
[sourcecode language='cpp']
BOOL WINAPI SetThreadPriority( __in HANDLE hThread, __in int nPriority );
[/sourcecode]
Note that you can’t set the the thread priority when you’re creating a thread using CreateThread API. Also, windows doesn’t provide no function to get a thread’s priority because operating system may dynamically boost thread’s priority at special situations (e.g keyboard inputs).
Relative priority of threads
Even we’ve different level of priorities for threads; it’s still related to priority of the running process. The actual range of priorities is 0-31. None of the time-critical threads in a normal priority process can attain priority level 31. The operating system maps the thread based on the process’s priority. For example a process having priority class high can attain a maximum thread priority of 15 even we set the thread priority to time critical. While a thread in a process having real-time priority can gain thread priority level 31.
Developers should not work with numerical values of priority, instead use priority class symbolic names to achieve the desired priority because the range process priorities and other related specification can be changed in the future releases of the operating systems.
As a last point. Thread and process priority should not make you confused. Actually the threads in a process doing the real work and process is actually inert but it providing context for the execution.