SetThreadPriority

  BOOL SetThreadPriority(hThread, nPriority)    
  HANDLE hThread;    
  int nPriority;    

The SetThreadPriority function sets the priority for the specified thread.

Parameters

hThread

Supplies a handle to the thread whose priority is to be set. The handle must have been created with THREAD_SET_INFORMATION access.

nPriority

Supplies the priority value for the thread. The following five priority values (ordered from lowest priority to highest priority) are allowed.

Priority Meaning

THREAD_PRIORITY_LOWEST  
  The thread's priority should be set to the lowest possible settable priority.
THREAD_PRIORITY_BELOW_NORMAL  
  The thread's priority should be set to just below normal.
THREAD_PRIORITY_NORMAL  
  The thread's priority should be set to the normal priority value. This is the value that all threads begin execution at.
THREAD_PRIORITY_ABOVE_NORMAL  
  The thread's priority should be set to just above normal priority.
THREAD_PRIORITY_HIGHEST  
  The thread's priority should be set to the highest possible settable priority.

Return Value

The return value is TRUE if the function is successful. Otherwise, it is FALSE in which case extended error information is available from the GetLastError function.

A thread's priority may be set using SetThreadPriority. This call allows the thread's relative execution importance to be communicated to the system. The system normally schedules threads according to their priority. The system is free to temporarily boost the priority of a thread when significant events occur (e.g. keyboard or mouse input . . . ). Similarly, as a thread runs without blocking, the system will decay its priority. The system will never decay the priority below the value set by this call.

In the absence of system originated priority boosts, threads will be scheduled in a round-robin fashion at each priority level from THREAD_PRIORITY_HIGHEST to THREAD_PRIORITY_LOWEST. Only when there are no runnable threads at a higher level, will scheduling of threads at a lower level take place.

All threads initially start at THREAD_PRIORITY_NORMAL.

If for some reason the thread needs more priority, it can be switched to THREAD_PRIORITY_ABOVE_NORMAL. Switching to THREAD_PRIORITY_HIGHEST should only be done in extreme situations. Since these threads are given the highes priority, they should only run in short bursts. Running for long durations will soak up the systems processing bandwidth starving threads at lower levels.

If a thread needs to do low priority work, or should only run there is nothing else to do, its priority should be set to THREAD_PRIORITY_BELOW_NORMAL or THREAD_PRIORITY_LOWEST.

Care must be taken when manipulating priorities. If priorities are used carelessly (every thread is set to THREAD_PRIORITY_HIGHEST), the effects of priority modifications can produce undesirable effects (e.g. starvation, no effect . . . ).

See Also

GetThreadPriority