Search code examples
c++multithreadingwinapireal-timethread-priority

How to increase process/thread priority more than 15 (high)


I have an Intel processor with 8 physical cores (16 with hyper-threading), and I am trying to fully utilize four of these cores by increasing the priority of my C++ application's process and the priority of four threads I am starting in this application (I am using OpenMp to lunch threads).

According to Microsoft's priority table:

Process priority class Thread priority level Base priority
HIGH_PRIORITY_CLASS THREAD_PRIORITY_IDLE 1
THREAD_PRIORITY_LOWEST 11
THREAD_PRIORITY_BELOW_NORMAL 12
THREAD_PRIORITY_NORMAL 13
THREAD_PRIORITY_ABOVE_NORMAL 14
THREAD_PRIORITY_HIGHEST 15
THREAD_PRIORITY_TIME_CRITICAL 15
REALTIME_PRIORITY_CLASS THREAD_PRIORITY_IDLE 16
THREAD_PRIORITY_LOWEST 22
THREAD_PRIORITY_BELOW_NORMAL 23
THREAD_PRIORITY_NORMAL 24
THREAD_PRIORITY_ABOVE_NORMAL 25
THREAD_PRIORITY_HIGHEST 26
THREAD_PRIORITY_TIME_CRITICAL 31

When I do the following, I only see priority set to 15:

int priority;
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
priority = GetThreadPriority(GetCurrentThread());

Also, checking the priority from the Task Manager, it is set to high, not real-time.

How can I increase the priority to more than 15?


Solution

  • For a default windows installation you need to have Administrator-Privileges to increase the priority class of a process to REALTIME_PRIORITY_CLASS.

    The privilege you need to have for this is called SE_INC_BASE_PRIORITY_NAME:

    Contant/value Description
    SE_INC_BASE_PRIORITY_NAME
    TEXT("SeIncreaseBasePriorityPrivilege")
    Required to increase the base priority of a process.
    User Right: Increase scheduling priority.

    By default this privilege is only given to members of the Administrator Group,
    but you can use the Increase scheduling priority group policy to also grant normal users this privilege.


    Assuming you're running as a user that has access to said privilege (by default only members of the Administrators Group), and the privilege is enabled for the current process, then you should be able to increase your processes priority to realtime with:

    SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    

    Why Realtime Priority is usually a bad idea

    Keep in mind though that realtime priority does really mean realtime.

    Almost all system processes run at a lower priority than realtime - so if your process decides to do some heavy computations it'll completely block the rest of your system from doing anything.

    Scheduling Priorities
    Use HIGH_PRIORITY_CLASS with care. If a thread runs at the highest priority level for extended periods, other threads in the system will not get processor time. The high-priority class should be reserved for threads that must respond to time-critical events. [...]
    The important point is that a high-priority thread should execute for a brief time, and only when it has time-critical work to perform.

    You should almost never use REALTIME_PRIORITY_CLASS, because this interrupts system threads that manage mouse input, keyboard input, and background disk flushing. This class can be appropriate for applications that "talk" directly to hardware or that perform brief tasks that should have limited interruptions.

    Recommended read: When you set a 100% CPU program to real-time priority, you get what you asked for

    For almost all tasks its usually best to leave the priority at its default (NORMAL_PRIORITY_CLASS - THREAD_PRIORITY_NORMAL) - you'll still get plenty of cpu time, but you don't starve out any other applications that also need some.

    • If you have an application that is truly time critical you can temporarily increase your priority to high.
    • realtime priority should only ever be used if you're actually talking to a hardware device that requires precise timing.