C # threads-Windows scheduling thread principles (3)

Source: Internet
Author: User

Windows itself is a preemptible operating system.AlgorithmFor example, when to schedule threads and how long it takes.

We always use WindowsProgramPersonnel, it is necessary for us to know the algorithm closest to us.

Why? Why does Windows respond so quickly to commands issued by the system, information retrieval, and other operations? Is this only the 30 ms credit for context switching? The operating system can process the most urgent requests of current users according to their operations and give responses in the shortest time. We should know these reasons.

Someone will mention that this is the credit of the thread, right. This is the credit of the thread. When you are operating, the thread is processing your request. Now let's take a look at the attributes of the thread?

Open the Start Menu, enter spy directly in the search box, and a program named spy ++ will pop up to run spy ++. This is the full version of vs2010.

This is an interesting thing. You may try it out. Now let's take a look at what this thing can do.

 

I found a QQ thread window, right-click the menu, and select-> message.

When a box is displayed, the data will be refreshed all the time, and then the QQ page will be displayed? Hover your mouse over QQ twice and you will find it carefully. Is the screen-flushing message quite appealing? Again, you will look at the attribute options. You will also see the process ID. An application is also a process, a thread related to QQ, and all process IDs will be the same. Every time you perform an operation, Windows does a job that is beyond your imagination. A try may surprise you. After reading the message, can I monitor the thread? In spy ++, select monitoring> thread

I found the QQ thread:

The context switch records the number of windows context switches. "2453261" indicates the number of times the system has called the QQ thread. What you see is the attributes of a thread.

Now you may have questions again. Why?

The first two articles in the same seriesArticleEvery thread has its own attributes.Among kernel objects, All containContext StructureThe context structure is used to reflect the status of the thread cup register during the last thread execution. At any time,Windows only uses one threadCodeAllocate to a CPU. A thread can run only one time slice.After the "time slice" of the thread ends, Windows checks all existing thread kernel objects and schedules only those threads that are not waiting. In Windows, select a schedulable thread Kernel Object and switch to it.

Windows has a unique set of standards for selecting a schedulable thread. Do you see the priority of the thread in it? The current priority and basic priority change as the program is enabled or not.

A thread allows a time slice to run. That's right. However, the rule of the Windows execution thread does not have much to do with the time slice. The thread can be stopped at any time during running, and then another thread is scheduled by windows. You have some control, to control the thread you want to run, but there is not much control, it is better not to control it.

Remember one thing about thread execution:

You cannot guarantee that your threads are running all the time, and you cannot prevent other threads from running.

Windows is not a real-time operating system. If you want to know more about it, go and dig deeper. I will not write much, so that you will not get out of the scope (-.-|.

Now let's focus on the thread priority,Knowledge points that must be understood when you want to become a good programmer.

Thread priority level 0 ~ 31. In Windows, threads are scheduled in turn in a high-to-low scheduling mode. If a thread with a priority of 31 is finished, Windows will find the next idle thread, if there is a thread with 31 levels in the idle thread, Windows will hand over 31 levels of threads to the cup for processing.

Someone has to ask, if there is a thread level of 31, isn't it that low-level threads won't be called in windows? This is unfair.

Indeed, Windows is unfair. There are high-level threads and low-level threads have no chance to request to use the cup.

The CPU is running a low-level thread. What if a high-level thread requests to call the CPU with a higher priority than the running low-level thread?

Windows will not hesitate to drive away Running low-level threads, and then put high-level threads to use the CPU.

High-priority threads always occupy lower-level threads. Is there any sense of weakness?

Okay. Let's take a look at the calling level,How to Design the running Thread level of an applicationRight.

When designing an application, you should feel that your application needs a larger or smaller response capability than other applications running on the machine at the same time, and then selectProcess Priority(Note ).

Why should we introduce process priority classes?

Microsoft has realized that it is difficult for developers to assign process priorities completely and reasonably. You do not know the priority of the process. To solve this issue, windows exposes an abstraction layer of the priority class. In order to reflect your decision, Windows supports six process priority classes: idel, below normal, normal, above normal, hight, and realtime (ascending ), normal is the default process priority, so it is the most common.

If an application runs without doing anything in the system, it is suitable to assign an idel priority class. It is estimated that QQ's exit status and screen saver are designed in this way. Of course there are many more ....

During process grading, the most critical factors should be taken into account. Instead of interfering with other more critical tasks, the hight priority class should be used only when necessary, the highest realtime priority is generally avoided, because it is quite high, to the 31 level, it may even interfere with operating system tasks, such as hindering some network transmission, disk read/write, etc, in addition, the thread of the realtime process may be unable to process the keyboard and mouse input in a timely manner, and the user may feel that his computer has crashed. there must be good reasons to use realtime priority, such as responding to some hardware events with short latency.

After selecting a process priority class, you don't have to worry about your programs and other applications. Now, focus onApplication Thread:

Windows supports 7Relative thread priority: Idel, lowest, below normal, normal, abve normal, highest, and time-critical.

These are relative to the process priority, and normal is still the default, which is the most commonly used. For example, we use a mall as an example:

This figure is barely eye-catching.

Different apparel brands have different priorities.

The prices of apparel under each brand are different, indicating that their thread priorities are different.

Such as ad and Nike, which have a high process priority and a high thread priority. Because they are more expensive than ordinary brands.

The brand level determines the clothing price. The process priority level determines the relative thread priority level! After this example, the following table can be understood:

Relative thread priority Process Priority
Idle Below normal Normal Above normal High Realtime
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

Remember:

If you change the priority class of a process, the relative priority of the thread will not change, but its priority will change..

Maybe you may have doubts about the numbers in these tables. This represents the 0 ~ 31. Why is there no "0" in this table?

"0" is available at this level, but it is retained to the zero-page thread. What is a zero-page thread? At startup, the system creates a special thread named "zero-page thread". It is the only thread with a thread level of 0 in the system. It is auxiliary to the execution of no other processes, clears all idle pages of the system's Ram. There are still some numbers in the Table: 17,18, 19,20, 21,27, 28,29, 30. These thread levels cannot be obtained by user mode. These priorities can be obtained only when writing a device driver running in kernel mode.

Note that the priority of a realtime thread cannot be lower than 16. Similarly, a thread with a non-Realtime priority cannot be higher than 15.

 

Are process priority classes and relative thread priorities clearly classified?This concept is easy to confuse. You may think that Windows can schedule a process. However, Windows will never schedule a process. It only schedules a thread, the process priority class is an abstract concept proposed by Microsoft to help you understand the relationship between your application and other running applications. It has no other purpose.

Let's look at a design example:

Now there is a thread to design. It is used for computing restricted tasks that run for a long time, such as compiling code, Pinyin checking, and spreadsheet computing. Generally, the priority of this thread is reduced, rather than the priority of the thread is increased. If you want to design a fast response time, run for a short period of time, and then reply to the waiting state, you should increase the priority of the thread. The rule summarized here is, A high-priority thread should be in the waiting state most of its life, so as not to affect the overall response capability of the bear.

Click the Windows menu key in the lower right corner of the keyboard to see the effect? Windows menus immediately seize other low-level threads and display their menus. When a user moves up or down a menu, the menu thread quickly responds to each button or mouse movement. This is a good example of a high-priority thread that stops running after the user closes the menu.

You can change the relative priority of the thread. The priority attribute in the thread will pass it one of the five values defined in the threadpriority Enumeration type, that is, the gray columns in the above table.

Windows reserves priority 0 and realtime for itself, and CLR reserves idle and time-critical priority for itself.

The CLR Terminator thread runs at the time-critical priority. Developers do not need to use these priorities, but it is good to know about them.

The existence of priority allows applications to process user requests in a more user-friendly manner. This design is quite good. Without it, we cannot control command machines at will.

 

The thread basics are just finished. as developers, we should know that the thread is a very valuable resource and must be saved for use. To do this, the best way is to use the threadpool of the thread pool.

In this next article, we have to say that the thread pool automatically manages the creation and destruction of threads, which is very good. (^. ^) Y -~~

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.