4
\$\begingroup\$

This code snippet is about asking Windows for the highest possible execution priority. Basically, we have to ask for real-time priority class for the entire process, after which each thread has to ask for real-time thread priorities. However, since Windows Vista, whatever is the program that needs the real-time priority, it must be asked to be run in administrator privilege level.

The demonstration program I provide simply loops for 7 seconds. During that period, your system should be frozen (try pressing Caps Lock; should not switch the Caps Lock light). Also, if you put on some audio, the sound card will repeat its contents of the sound buffer over and over again.

rtprio.h:

#pragma once
#include <Windows.h>

BOOL RealTimePriorityClass()
{
    return SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
}

BOOL RealTimeThreadPriority()
{
    return SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
}

main.cpp:

#include <windows.h>
#include "rtprio.h"

static const DWORD dwInterval = 7000; // millseconds.

DWORD WINAPI ThreadProc(LPVOID lpParams);

int WINAPI WinMain(HINSTANCE hInst,
    HINSTANCE hPrev,
    LPSTR lpCmdLine,
    int nShowCmd)
{
    DWORD       i;
    DWORD       dwInitTicks;
    DWORD       dwCores;
    SYSTEM_INFO sysInfo;

    GetSystemInfo(&sysInfo);
    dwCores = sysInfo.dwNumberOfProcessors;

    // Ask real-time priority class for the entire process.
    RealTimePriorityClass();

    for (i = 0; i < dwCores - 1; i++)
    {
        CreateThread(NULL,
                     0,
                     ThreadProc,
                     NULL,
                     0,
                     NULL);
    }

    RealTimeThreadPriority();

    dwInitTicks = GetTickCount();

    while (GetTickCount() - dwInitTicks < dwInterval);

    return 0;
}

DWORD WINAPI ThreadProc(LPVOID lpParams)
{
    // Ask for real-time priority for this thread.
    RealTimeThreadPriority();

    // Freeze the system for dwInterval milliseconds.
    while (TRUE) {}

    return 0;
}
\$\endgroup\$
5
  • \$\begingroup\$ I'm not sure it is wise to ask us to test code that will lock up your PC for 7 seconds... :p \$\endgroup\$
    – Dan
    Commented Oct 26, 2015 at 9:36
  • 4
    \$\begingroup\$ You scared? ;-) \$\endgroup\$
    – coderodde
    Commented Oct 26, 2015 at 9:38
  • \$\begingroup\$ @DanPantry Since there is a warning label attached, I'm all for it. \$\endgroup\$
    – Mast
    Commented Oct 26, 2015 at 9:38
  • 2
    \$\begingroup\$ I don't understand what the question is? \$\endgroup\$ Commented Oct 26, 2015 at 12:32
  • \$\begingroup\$ Tell me anything that comes to mind. \$\endgroup\$
    – coderodde
    Commented Oct 26, 2015 at 12:34

1 Answer 1

3
\$\begingroup\$

Works as intended.
But I get a thread priority of 15 rather than the expected 31 for a REALTIME_PRIORITY_CLASS?
See SetThreadPriority.

It seems that the THREAD_PRIORITY_TIME_CRITICAL is defined to THREAD_BASE_PRIORITY_LOWRT - 1, which could be further elevated to 31 if needed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.