Windows priority category

Change thread priority

In Windows, each thread is associated with a priority setting. The priority of the thread determines how much CPU time the thread receives. Low-priority threads receive less time, and high-priority threads receive more time. Of course, the amount of CPU time a thread receives has a profound impact on its execution performance and its interaction with other threads currently executing in the system.

In Windows, the thread priority setting is a combination of two values: the overall priority category of the process and the priority setting of each thread relative to this priority category. In other words, the actual priority of the thread is determined by the combination of the priority category of the process and the priority of each thread. I will talk about them one by one later.

1. Priority category

By default, processes have a common priority category, and most programs maintain this common priority category during the life cycle of their execution. Although the priority categories are not changed in the examples in this chapter, for the sake of completeness, a brief overview of the thread priority categories is given here. REALTIME_PRIORITY_CLASS

HIGH_PRIORITY_CLASS

ABOVE_NORMAL_PRIORITY_CLASS

NORMAL_PRIORITY_CLASS

BELOW_NORMAL_PRIORITY_CLASS

IDLE_PRIORITY_CLASS

By default, the priority category of the program is NORMAL_PRIORITY_CLASS. Normally, you do not need to change the priority category of the program. In fact, changing the priority category of a process has a negative impact on the performance of the entire computer system. For example, if you increase the priority class of a program to REALTIME_PRIORITY_CLASS, it will dominate the CPU. For some special applications, it may be necessary to increase the priority category of the application, but it is usually not necessary. As mentioned earlier, the application in this chapter does not change the priority category.

When you really need to change the priority class of the program, you can call SetPriorityClass(). You can call GetPriorityClass() to get the current priority class. The prototypes of these two functions are as follows:

DWORD GetPriorityClass(HANDLE hApp);

BOOL SetPriorityClass(HANDLE hApp, DWORD priority);

Here, hApp is the handle of the process. GetPriorityClass() returns the priority class of the application, or 0 if it fails. For SetPriorityClass(), priority specifies the new priority class of the process.

2. Thread priority

For a given priority class, the priority of each thread determines how much CPU time it receives in the process. When a thread is first created, it has a normal priority, but you can change the priority of the thread-even while it is executing.

The priority setting of the thread can be obtained by calling GetThreadPriority(). You can use SetThreadPriority() to increase or decrease the priority of a thread. The prototypes of these two functions are as follows:

BOOL SetThreadPriority(HANDLE hThread, int priority);

int GetThreadPriority(HANDLE hThread);

For these two functions, hThread is the handle of the thread. For SetThreadPriority(), priority is the new priority setting. If an error occurs, the return value is 0; otherwise, it returns a non-zero value. GetThreadPriority() will return the current priority setting. The priority settings are shown in Table 3-1 in descending order.

Table 3-1 Priority setting

 

 

Thread priority

 

value

 

THREAD_PRIORITY_TIME_CRITICAL

 

15

 

THREAD_PRIORITY_HIGHEST

 

2

 

THREAD_PRIORITY_ABOVE_NORMAL

 

1

 

THREAD_PRIORITY_NORMAL

 

0

 

THREAD_PRIORITY_BELOW_NORMAL

 

-1

 

THREAD_PRIORITY_LOWEST

 

-2

 

THREAD_PRIORITY_IDLE

 

-15

 

 

These values ​​increase or decrease relative to the priority category of the process. By combining the priority category of the process and the priority of the thread, Windows provides support for 31 different priority settings to the application.

If an error occurs, GetThreadPriority() returns THREAD_PRIORITY_ERROR_RETURN.

In most cases, if a thread has a normal priority category, you can change its priority setting at will without worrying about having a catastrophic impact on the performance of the entire system. You will see that in the thread control panel developed in the following part, you can change the priority setting of the thread in the process (but you cannot change the priority category).

Guess you like

Origin blog.csdn.net/wyyy2088511/article/details/108798171