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. In this post, you’ll get answers to these questions:
- How I set runtime process priority on my Windows C++ application?
- Can I change the process priority level at runtime?
- May I apply specific process priorities to 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 an efficient way?
By learning how To Set Runtime Process Priority On Windows In A C++ App, it will help you to build C++ applications with the use of a C++ IDE.
Table of Contents
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:
1 2 3 |
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,
1 2 3 4 5 |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
//--------------------------------------------------------------------------- #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++ ) { // Test your method or function MAXIT times here } 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.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition