How do I control thread priority in Windows NT?

Since Windows NT is not a full real-time system, there are things taking place below the surface. This obviously interferes with interrupts. When you create a thread, it returns a handle to the thread. You use that handle to call the function:

   BOOL SetThreadPriority(
      HANDLE hThread, // handle to the thread
      int nPriority // thread priority level
   ); 

Options for thread priority are:

  • THREAD_PRIORITY_ABOVE_NORMAL -- Indicates 1 point above normal priority for the priority class.
  • THREAD_PRIORITY_BELOW_NORMAL -- Indicates 1 point below normal priority for the priority class.
  • THREAD_PRIORITY_HIGHEST -- Indicates 2 points above normal priority for the priority class.
  • THREAD_PRIORITY_IDLE -- Indicates a base priority level of 1 for IDLE_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS processes, and a base priority level of 16 for REALTIME_PRIORITY_CLASS processes.
  • THREAD_PRIORITY_LOWEST -- Indicates 2 points below normal priority for the priority class.
  • THREAD_PRIORITY_NORMAL -- Indicates normal priority for the priority class.
  • THREAD_PRIORITY_TIME_CRITICAL -- Indicates a base priority level of 15 for IDLE_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS processes, and a base priority level of 31 for REALTIME_PRIORITY_CLASS processes.

Check the priority of the timer interrupt thread. Usually we set thread priority at THREAD_PRIORITY_NORMAL or THREAD_PRIORITY_ABOVE_NORMAL. You can increase priority to THREAD_PRIORITY_HIGHEST or THREAD_PRIORITY_TIME_CRITICAL.

You should also look at the thread priority class. Set the thread priority class using the function:

   BOOL SetPriorityClass(
      HANDLE hProcess, // handle to process
      DWORD dwPriorityClass // priority class
   ); 

where the options for priority class are:

  • ABOVE_NORMAL_PRIORITY_CLASS -- Windows 2000: Indicates a process that has priority above NORMAL_PRIORITY_CLASS but below HIGH_PRIORITY_CLASS.
  • BELOW_NORMAL_PRIORITY_CLASS -- Windows 2000: Indicates a process that has priority above IDLE_PRIORITY_CLASS but below NORMAL_PRIORITY_CLASS.
  • HIGH_PRIORITY_CLASS -- Specify this class for a process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class application can use nearly all available CPU time.
  • IDLE_PRIORITY_CLASS -- Specify this class for a process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes.
  • NORMAL_PRIORITY_CLASS -- Specify this class for a process with no special scheduling needs. REALTIME_PRIORITY_CLASS -- Specify this class for a process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.

Caution: You can lock your system by setting the priority too high. If the timer thread locks up there may be no way to halt it. It can also block other threads and processes from running. The August 2001 issue of the Windows Developer's Journal has an article on "Managing Thread Termination" that may be helpful if you have problems shutting down the timer thread function.