SetThreadPriority and SetPriorityClass
multithreading, windows
Solution
The process priority class and the thread priority are building the `base priority` of 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 the `base priority` can have various values, determined by the `thread priority`.
The `PROCESS_MODE_BACKGROUND_BEGIN` value for `SetPriorityClass` and the `THREAD_MODE_BACKGROUND_END` value for `SetThreadPriority` are 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 `SetPriorityClass` will set the process with all of its threads into `background processing mode`. The following `SetThreadPriority` will only release the a thread from `background processing mode`. But all other possible threads of the process will stay in in background processing mode.
Note: Only the combination of `process priority class` and `thread priority` determines the `base priority`. Therefore neither a call to `GetThreadPriority` nor a call to `GetPriorityClass` will return the base priority. Only their combination releases the base priority which is described in the "Scheduling Priorities" link above. Unfortunately the new `background processing mode` values aren't yet included in the `base priority` list. But the name `base priority` tells 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 to `fine tune` the scheduling priority. Another way are Priority Boosts. The priority boost functionality exists for some time. The new access to `background processing mode` values for `SetThreadPriority` and `SetPriorityClass` opens the priority boost capability directly. In Windows XP this had to be done by a call to SetProcessPriorityBoost.
Problem
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.