C++Builder How To Set Runtime Process Priority On Windows In A C++ App

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Set Runtime Process Priority On Windows In A C++ App
By Yilmaz Yoru November 11, 2021

How I can set runtime process priority on my Windows C++ application? Can I change the process priority level on runtime? May I apply specific process priorities in different actions? How can use full power of one of my CPU core or thread? How can I use maximum power of my CPU in efficient way? Let’s try to answer these questions.

The C++ Builder VCL library has a lot of Windows specific libraries for controlling the runtime behavior of our applications. We can easily change the process priority of the current task in thread/core.

What is the SetPriorityClass method in C++?​

SetPriorityClass method sets the priority class for the specified process and included in processthreadsapi.h library. A handle to the process and the priority class for the process should be given as a parameter. This class value together with the priority value of each thread of the process determines each thread’s base priority level.

What is the syntax of the SetPriorityClass method in C++?​

Here is the syntax of the SetPriorityClass method.

Syntax:
C++:
BOOL SetPriorityClass( HANDLE handle_process,  DWORD  priority_param );
Here,

  • the handle_process parameter is the handle to process, we can directly use GetCurrentProcess() method as a parameter to set the priority of the current process
  • and priority_param is the priority class parameter for the given process.

What are the parameters which can be used with the SetPriorityClass method ?​

In this SetPriorityClass() method we can use; REALTIME_PRIORITY_CLASS , NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS , PROCESS_MODE_BACKGROUND_BEGIN, PROCESS_MODE_BACKGROUND_END parameters.

ABOVE_NORMAL_PRIORITY_CLASS
0x00008000
Process that has priority above NORMAL_PRIORITY_CLASS but below HIGH_PRIORITY_CLASS.
BELOW_NORMAL_PRIORITY_CLASS
0x00004000
Process that has priority above IDLE_PRIORITY_CLASS but below NORMAL_PRIORITY_CLASS.
HIGH_PRIORITY_CLASS
0x00000080
Process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class application can use nearly all available CPU time.
IDLE_PRIORITY_CLASS
0x00000040
Process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes.
NORMAL_PRIORITY_CLASS
0x00000020
Process with no special scheduling needs.
PROCESS_MODE_BACKGROUND_BEGIN
0x00100000
Begin background processing mode. 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.This value can be specified only if hProcess is a handle to the current process. The function fails if the process is already in background processing mode.Windows Server 2003 and Windows XP: This value is not supported.
PROCESS_MODE_BACKGROUND_END
0x00200000
End background processing mode. The system restores the resource scheduling priorities of the process (and its threads) as they were before the process entered background processing mode.This value can be specified only if hProcess is a handle to the current process. The function fails if the process is not in background processing mode.Windows Server 2003 and Windows XP: This value is not supported.
REALTIME_PRIORITY_CLASS
0x00000100
Process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.

Is there a simple example of how to use the SetPriorityClass method in C++?​

Here is the simple example for the SetPriorityClass Method,
C++:
SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS );
// do real-time process here
SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );

Is there a longer example of using the SetPriorityClass method in C++?​

Here is the full example that shows how to use SetPriorityClass Method before and after heavy calculations,
C++:
//---------------------------------------------------------------------------
 
#include <vcl.h>
#include <chrono>
 
#pragma hdrstop
 
#include "Optimization_Arithmetics_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 
#define MAXIT  10000000000
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)  : TForm(Owner)
{
 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
 
  //--------------------------------------------
  auto start = std::chrono::steady_clock::now();
  for(  i = 0; i<MAXIT; i++ )
  {
 //
  }
  auto end = std::chrono::steady_clock::now();
  std::chrono::duration<double> diff0 = end-start;
  Memo1->Lines->Add("Empty:"+ FloatToStr(diff0.count()) );
 
  //--------------------------------------------
 
  start = std::chrono::steady_clock::now();
  for(  i = 0; i<MAXIT; i++ )
  {
 z += j*x;
  }
  end = std::chrono::steady_clock::now();
  diff = end-start;
  Memo1->Lines->Add( L"Elapsed: "+ FloatToStr(diff.count() - diff0.count() ) );
  Application->ProcessMessages();
 
  SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );
}
//---------------------------------------------------------------------------
Please be careful when you use REALTIME_PRIORITYCLASS! Using this flag the SetPriorityClass method tells Windows that a thread should use the highest possibly priority. That means if you have a lot of cores/threads you might completely monopolize the CPU core. In multi-tasking applications this may result full use of all available CPU cores resulting in the computer and Windows operating system becoming unresponsive due to there being no available time left for the CPU to do anything else. This may also cause high temperatures on the CPU and even burn out the CPU or motherboard if the cooling system is not designed for prolonged maximum CPU usage.