Windows via C/C ++ study notes -- thread priority

Source: Internet
Author: User

Each thread has a "Priority" ranging from 0 ~ 31, 0 is the lowest priority, and 31 is the highest priority. When the system determines which thread needs to be scheduled, first check whether a scheduling thread with a priority of 31 exists. If yes, select one for scheduling. When the time slice of this thread arrives, the system checks whether another scheduling thread with a priority of 31 exists. If so, it is scheduled.

As long as a thread with a scheduling priority of 31 exists, the system will never set the scheduling priority to 0 ~ 30 threads, this will cause other threads to "hunger ".

High-priority threads often interrupt the execution of low-priority threads. For example, when a thread with a priority of 15 is running, if the system finds a thread with a higher priority than 15 can be scheduled, the high-priority thread will interrupt the execution of the low-priority thread, even if the low-priority time slice is halfway through.

In addition, when the system boots, the system creates a special thread called "Zero page" (0 page) thread, which is the only thread in the system with a priority of 0 (lowest). When the system does not have any threads to execute, this thread is responsible for clearing all the ram pages in the system (that is, Resource Recycling ).

 

The thread priority has an abstract layer concept.

For some historical reasons, Microsoft has not completely fixed the behavior of the thread scheduler. Microsoft did not allow applications to take full advantage of the features of the scheduler. Microsoft claims that the behavior of this scheduler is a change and needs to be noticed during programming.

As you can see from these points, your application can have its own scheduling features.

Windows API fully reflects an abstraction layer of system scheduling. In this way, it will not directly communicate with the system scheduler. On the contrary, you can call the API function to convert these parameters according to different versions of the system. This layer is the abstraction layer of the thread priority.

The following describes in detail what the abstract layer has.

 

For a process, Windows has a "priority class" concept. These priority classes work with all threads in the process. Windows 2000/XP/2003/Vista supports six "priority classes ":

1. Real-time: Real-Time

2. High: high

3. above normal: higher than standard

4. Normal: Standard

5. below normal: lower than standard

6. Idle: idle.

A process should avoid using a "real-time" priority class, because using this priority class will make it difficult for threads in other processes to be scheduled, and even interrupt or seize the execution of system threads. The "high" priority class should also be avoided unless this priority is required for special work.

When a process's "priority class" is determined, you only need to consider the priority relationship between various threads in the process.

For threads in a process, there is a concept of "relative thread priority", which can determine the priority relationship between multiple threads in a process.

Windows supports 7 "relative thread priorities ":

1. time-critical: critical time (highest relative thread priority)

2. heightest: highest (translation is like this, but not the highest relative thread priority)

3. above normal: higher than standard

4. Normal: Standard

5. below normal: lower than standard

6. Lowest: Lowest (translation is like this, but not the lowest relative thread priority)

7. Idle: idle

This does not mention 0 ~ 31. Developers never set the priority of a thread, that is, they do not need to set the priority of a thread to 0 ~ 31. The operating system maps the "priority class" and "relative thread priority" to a specific priority. This ing method varies with the Windows version.

The thread priority ing method for Windows 2000/XP/2003/Vista is as follows (Vista is used in the book, but compared with the fourth version of this book, we can see that 2000 and Vista are the same ):

Thread relative

Priority

Process Priority 

Idle

Below normal

Normal

Above normal

High

Real-time

Time-critical

15

15

15

15

15

31

Highest

6

8

10

12

15

26

Above normal

5

7

9

11

14

25

Normal

4

6

8

10

13

24

Below normal

3

5

7

9

12

23

Lowest

2

4

6

8

11

22

Idle

1

1

1

1

1

16

 

Check the table carefully and now you know why it is better not to set the "process priority class" to "real-time", because if a process has a "real-time" priority class, the priority of all threads in the process (16 at the lowest) is higher than that of the threads in any process with other priority classes (15 at the highest. In this way, threads in other processes with lower priority levels than "real-time" cannot be scheduled.

 

It should be noted that the "priority class" is an abstract concept for a process, but it does not mean that the process can be scheduled, and only the thread can be scheduled. Microsoft only proposed this concept to help you differentiate it from the internal running of the scheduler. The "priority class" should be able to affect the priority of all threads in a process.

 

The content about the thread priority is described above, including the "specific priority" of the thread and the content of the "Priority abstraction layer" (process "priority class" and "thread relative priority ).

The following describes how to set the thread priority in programming.

 

A process is often associated with a "priority class". You can set the specific content of this priority class in the fdwcreate parameter of the CreateProcess function. There are 6 options for the following priority classes:

1. realtime_priority_class: Real-time, real-time priority class

2. high_priority_class: High, high priority class

3. above_normal_priority_class: above normal, higher than standard

4. normal_priority_class: Normal, standard

5. below_normal_priority_class: below normal: lower than standard

6. idle_priority_class: idle, idle

 

You can also change a specific process priority class, which can be achieved by calling the setpriorityclass function.

Bool setpriorityclass (
Handle hprocess, // the Specified Process Handle
DWORD fdwpriority); // priority class (corresponding to the above 6 values)

For example, the following code sets the priority class of the current process to idle:

Setprioriytclass (getcurrentprocess (), idle_priority_class );

 

Of course, you can obtain the priority class of a process by calling the getpriorityclass function:

DWORD getpriorityclass (handle hprocess );

The value returned by this function is one of the values corresponding to the six priority classes.

 

When a thread is created, its "relative thread priority" is normal (standard) by default ). The createthread function does not provide the function of setting the relative priority of a thread. You can call the setthreadpriority function to set the relative priority of a thread.

 

Bool setthreadpriority (
Handle hthread, // specifies the thread handle
Int npriority); // The relative thread priority (the value corresponds to the following)

This function takes a thread handle and the relative priority of the thread, and sets the relative priority of the corresponding thread. The relative priority of the thread is as follows:

1. thread_priority_time_critical: time-critical, critical time (maximum)

2. thread_priority_highest: highest, which is the highest (in fact, it is the "second high ")

3. thread_priority_abve_normal: above standard

4. thread_priority_normal: Normal, standard

5. thread_priority_below_normal: below normal, lower than standard

6. thread_priority_lowest: lowest, which is the lowest (in fact, it is "low ")

7. thread_priority_idle: idle, idle (lowest)

 

You can call the gettreadpriotiry function to obtain the relative priority of a specific thread.

Int getthreadpriority (handle hthread); // The function returns the preceding seven values.

To create a thread whose relative priority is not a standard thread, for example, to create a thread higher than the standard thread, you can pass the create_suincluded parameter to createthread to create a thread whose initial status is "suspended, then call the setthreadpriority function to set the relative priority of the thread, and then call the resumethread function to resume the running of the thread. The Code is as follows:

 

DWORD dwthreadid;
Handle hthread = createthread (null, 0, threadfunc, null,
Create_suincluded, & dwthreadid );
Setthreadpriority (hthread, thread_priority_above_normal );
Resumethread (hthread );
Closehandle (hthread );

 

The operating system dynamically increases the basic thread priority level (0 ~ 31), usually in response to some I/O events.

Sometimes, it is inconvenient for the system to dynamically increase the thread priority. You can call the setprocesspriorityboost and setthreadpriorityboost functions to notify the system whether to dynamically increase the thread priority.

Bool setprocesspriorityboost (
Handle hprocess,
Bool bdisablepriorityboost );
Bool setthreadpriorityboost (
Handle hthread,
Bool bdisablepriorityboost );

 

The second parameter of these two functions is used to notify the system whether to dynamically increase the priority of the specified process and the relative priority of the specified thread.

You can also use the following two functions to obtain the information:

Bool getprocesspriorityboost (
Handle hprocess,
Pbool pbdisablepriorityboost );
Bool getthreadpriorityboost (
Handle hthread,
Pbool pbdisablepriorityboost );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.