Thread priority

zhaozj2021-02-16  64

Priority is an important basis for thread schedule. High priority threads will always get favored by CPU. Of course, the operating system will adjust the priority of each thread depending on the situation. For example, the priority of the front desk thread should be raised, and the priority of the background thread should be lowered.

The priority range of thread ranges from 0 (minimum) to 31 (highest). When you generate threads, don't specify its priority directly, but use two steps. The first step is to specify the "Priority Class" to the process, the second step is to specify the thread that "relative priority" gives the process. Table 1 is a description of the priority level, which is specified in the DWCREATIONFLAGS parameter of CreateProcess. If you don't specify, the system default is Normal_Priority_Class, unless the parent process is idle_priority_class (then the child process will be idle_priority_class).

Equal level

Code

First level value

idle

IDLE_PRIORITY_CLASS

4

Normal

NORMAL_PRIORITY_CLASS

9 (front desk) or 7 (background)

HIGH

HIGH_PRIORITY_CLASS

13

Realtime

RealTime_Priority_Class

twenty four

Table 1 Priority level division of Win32 thread

■ The "IDLE" level is only performed when the CPU time will be wasted (i.e., iDLE TIME). This level is best suited for system monitoring software, or screen protection software.

■ "Normal" is the default level. The system can dynamically change the priority, but is limited to the "Normal" level. When making a front desk, the thread priority is increased to 9. When the process becomes a background, the priority is reduced to 7.

■ The HIGH level is to meet the needs of immediate reactions, such as the use of the user to take the workmanship (Task Manager) immediately.

■ The "realTIME" level is hardly used by the general application. Even the system controls the mouse, keyboard, drive state rescan, Ctrl Alt DEL, etc., is lower than the priority of "realTIME". This kind of use is "if it is not executed within a certain time range, the data is lost". This level must be used under the correct assessment. If you specify such a level to a general (not often blocking) thread, the multi-task environment will be paralyzed, because this thread has such a high priority, other threads There is no chance to be executed.

The above four levels, each level is mapped to a range of priority value, IDLE_ minimum, Normal_ times, high_, RealTime_ is the highest. In each level, you can use setthreadpriority to set exact priority and can be slightly or slightly lower than the normal value of the level (range is two points). You can imagine SETTHREADPRIORITY as a fine tuning operation.

SetthreadPriority parameters

Micromalance

Thread_Priority_lowest

- 2

Thread_Priority_BELOW_NORMAL

- 1

Thread_Priority_NORMAL

constant

Thread_Priority_Above_NORMAL

1

Thread_Priority_Highest

2

In addition to the above five fine-tuning, two fine adjustment constants can also be specified:

Setthreadpriority parameters

Facing any level of adjustment results

Faced with "RealTime" tuning results thread_priority_idle

1

16

Thread_Priority_Time_critical

15

31

These conditions can be summarized in Table 2.

Priority level

idle

Lowest

BELOW NORMAL

Normal

Above Normal

Highest

Time critical

idle

1

2

3

4

5

6

15

NORMAL (background)

1

5

6

Seduce

8

9

15

Normal (front desk)

1

Seduce

8

9

10

11

15

HIGH

1

11

12

13

14

15

15

Realtime

16

twenty two

twenty three

twenty four

25

26

31

Table 2 Win32 thread priority

转载请注明原文地址:https://www.9cbs.com/read-15434.html

New Post(0)