I don’t understand how to use SetThreadPriority and SetPriorityClass to lower and increase the priority of a Thread.
My understanding is that the SetPriorityClass selects the range of priorities available to a process and the SetThreadPriority sets the relative priority within the class.
For instance, what is the result of doing this for a thread :
SetPriorityClass(GetCurrentProcess(), PROCESS_MODE_BACKGROUND_BEGIN);
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
Thanks for help.
The process priority class and the thread priority are building the
base priorityof a thread. See Scheduling Priorities to find how the priorities are assembled. By looking at this list it becomes clear that your understanding is somewhat correct; within a certain priority class thebase prioritycan have various values, determined by thethread priority.The
PROCESS_MODE_BACKGROUND_BEGINvalue forSetPriorityClassand theTHREAD_MODE_BACKGROUND_ENDvalue forSetThreadPriorityare not supported on all Windows versions.PROCESS_MODE_BACKGROUND_BEGIN:
The system lowers the resource scheduling priorities of the process (and its threads) so that it can perform background work without significantly affecting activity in the foreground.
THREAD_MODE_BACKGROUND_END:
End background processing mode. The system restores the resource scheduling priorities of the thread as they were before the thread entered background processing mode.
The consequence of the scenario in question here is predictable: The
SetPriorityClasswill set the process with all of its threads intobackground processing mode. The followingSetThreadPrioritywill only release the a thread frombackground processing mode. But all other possible threads of the process will stay in in background processing mode.Note: Only the combination of
process priority classandthread prioritydetermines thebase priority. Therefore neither a call toGetThreadPrioritynor a call toGetPriorityClasswill return the base priority. Only their combination releases the base priority which is described in the “Scheduling Priorities” link above. Unfortunately the newbackground processing modevalues aren’t yet included in thebase prioritylist. But the namebase prioritytells what matters here: Based on the base priority (derived from process priority class and thread priority) the scheduler is allowed to dynamically adapt the scheduling priority. The background mode is just another way tofine tunethe scheduling priority. Another way are Priority Boosts. The priority boost functionality exists for some time. The new access tobackground processing modevalues forSetThreadPriorityandSetPriorityClassopens the priority boost capability directly. In Windows XP this had to be done by a call to SetProcessPriorityBoost.