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);
}

{
}


main.cpp:

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

static const DWORD dwInterval = 7000; // millseconds.

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++)
{
0,
NULL,
0,
NULL);
}

dwInitTicks = GetTickCount();

while (GetTickCount() - dwInitTicks < dwInterval);

return 0;
}

{

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

return 0;
}

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

But I get a thread priority of 15 rather than the expected 31 for a REALTIME_PRIORITY_CLASS?
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.