9

I am working on a program to sort data, and I need to to set the process to priority 31, which I believe is the highest process priority in Windows. I have done some research, but can't figure out how to do it in C++.

hyde
  • 50,653
  • 19
  • 110
  • 158
john
  • 101
  • 1
  • 1
  • 2
  • in Unix you have nice(int inc) in unistd.h, maybe something similar on Windows? – jcomeau_ictx Mar 07 '11 at 05:59
  • 1
    Just out of curiosity, why does a program that sorts data need to run at highest priority? – Jeremy Friesner Dec 22 '11 at 18:59
  • 7
    For future googlers: Setting a really high priority will not significantly improve the throughput of programs like this since it probably uses 99% CPU even on idle priority. A high priority is more useful for reducing latency. – Navin Apr 20 '13 at 05:17
  • 3
    Indeed, it is only when there is *contention* for the CPU that it even matters. So, please do not set your games to the Highest Priority class. I say that because it can cause a 'priority skew' with system dependencies it may rely on. – dyasta Jul 26 '17 at 23:15

3 Answers3

15

The Windows API call SetPriorityClass allows you to change your process priority, see the example in the MSDN documentation, and use REALTIME_PRIORITY_CLASS to set the highest priority:

SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)

Caution: if you are asking for true realtime priority, you are going to get it. This is a nuke. The OS will mercilessly prioritize a realtime priority thread, well above even OS-level input processing, disk-cache flushing, and other high-priority time-critical tasks. You can easily lock up your entire system if your realtime thread(s) drain your CPU capacity. Be cautious when doing this, and unless absolutely necessary, consider using high-priority instead. More information

John Weisz
  • 23,615
  • 9
  • 74
  • 118
lunixbochs
  • 19,084
  • 2
  • 33
  • 44
5

The following function will do the job:

void SetProcessPriority(LPWSTR ProcessName, int Priority)
{
    PROCESSENTRY32 proc32;
    HANDLE hSnap;
    if (hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
    if (hSnap == INVALID_HANDLE_VALUE)
    {

    }
    else
    {
        proc32.dwSize = sizeof(PROCESSENTRY32);
        while ((Process32Next(hSnap, &proc32)) == TRUE)
        {
            if (_wcsicmp(proc32.szExeFile, ProcessName) == 0)
            {
                HANDLE h = OpenProcess(PROCESS_SET_INFORMATION, TRUE, proc32.th32ProcessID);
                SetPriorityClass(h, Priority);
                CloseHandle(h);
            }
        }
        CloseHandle(hSnap);
    }
}

For example, to set the priority of the current process to below normal, use:

SetProcessPriority(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS)
Michael Haephrati
  • 2,849
  • 1
  • 27
  • 48
1

After (or before) SetPriorityClass, you must set the individual thread priority to achieve the maximum possible. Additionally, another security token is required for realtime priority class, so be sure to grab it (if accessible). SetThreadPriority is the secondary API after SetPriorityClass.

dyasta
  • 1,889
  • 15
  • 23