WINDOWS core programming experience-Thread Scheduling

Source: Internet
Author: User

There are five main states of threads

1. New status: A New thread object is created.

2. ready state: After the thread object is created, other threads call the start () method of the object. The thread in this status is located in the runable thread pool and becomes runable, waiting to obtain the CPU usage right.

3. Running status: the ready thread obtains the CPU and executes the program code.

4. Blocking status: the blocking status indicates that the thread gives up CPU usage for some reason and stops running temporarily. It is not until the thread enters the ready state that it has the opportunity to go to the running state.

5. suspension status: Generally, it refers to being suspended. because at the same time, there are more than one thread to be "synchronized", so based on the principle of time slice rotation, after occupying the CPU for a period of time, he is suspended and the thread environment is pressed to the stack.

The following describes pending and blocking:

1. suspension is an active action, so recovery should also be completed proactively, while blocking is a passive action, which is the performance of tasks while waiting for events or resources, you don't know when it will be blocked (pend), so you cannot know exactly when it will be restored. In addition, a suspended queue can be viewed as one in the operating system, while a blocked queue has its own queue for different events or resources such as semaphores.

2. Blocking pend) is to release the CPU of a task, and other tasks can run. It usually appears while waiting for a certain resource or semaphore. Suspend) does not release the CPU. If the task has a high priority, it will never be able to run other tasks. Generally, it is suspended for conditional interruptions in program debugging. When a certain condition occurs, it will be suspended, then perform one-step debugging.

3. pend is an event or message that a task takes the initiative to wait. suspend directly suspends a task. In the future, this task has nothing to do with you. Communication or synchronization between any tasks has nothing to do with this sudedtask, unless you resume task;

4. task Scheduling is implemented by the operating system. During task scheduling, tasks in the suspended state are directly ignored, but the tasks in the pend state are taken into account. When the resources waiting for tasks in the pend state are ready, you can convert it to ready. Ready only needs to wait for the CPU time. Of course, task scheduling also occupies the overhead, but it is not large and can be ignored. As you can understand, the operating system does not manage this task as long as the task is suspended.

5. suspension is active. Generally, you need to use the suspension function for operations. If there is no resume action, this task will never be ready. However, the resource is in sleep state because it is preemptible by other tasks. Both are expressed in a "clear" way from the readiness state, that is, the corresponding flag is cleared, but the implementation method is different.

These terms are for threads or processes. For example, there are three states of processes, but different operating systems, or different versions of the same operating system also have different state management methods, such as what "five-state process" and "Seven-state process, we can compare the operating system's control over processes to hiring employees.

1. Sleep: the employer said to the employee, "Go to bed, report back at a certain moment, and continue to work !"

2. Suspension: the employer said to the employee, "Go to bed. when you need it, I will call you and work on it !"

3. Readiness: the employee's broom refers to the CPU) is snatched by another employee, and he can only participate in the competition in the prescribed way, for example, in Linux, by time slice rotation ), grab your own, and then work!

4. Blocking Mode: The rag of a hired employee refers to other resources other than the CPU) is snatched by another employee, and he can only wait for it. When the rag arrives, it enters the ready state, continue to participate in the broom competition.

As you can imagine, sleep and suspension should be similar, and sleep should be how long it will take to come back, but they are always occupying the CPU.

WINDOWS is not a real-time operating system, but a preemptible multi-threaded operating system. The system can stop one thread and call another thread at any time.


A thread can be suspended multiple times, and the number of suspendthreads must be ResumeThread. Apart from being called by someone else, the thread can also tell the system to suspend itself within a period of time, scheduling is not required. This can call Sleep implementation.

void Sleep(DWORD dwMilliseconds);

Here, the parameter indicates how long the thread will be suspended, but WINDOWS is not a real-time operating system and cannot be guaranteed to wake up on time. When 0 is input for it, indicates that the main thread voluntarily abandons the remaining part of the time slice. Note: this time.


When the computing thread needs to execute a task, many people are used to using GetTickCount64

ULONG start=GetTickCount64();  //do something.ULONG end=GetTickCount64();

This function may become inaccurate because the thread is suspended halfway. Windows provides a function to return a thread to obtain the cpu time.

BOOL GetThreadTime(        HANDLE hThread,        PFILETIME pftCreationTime,        PFILETIME pftExitTime,       PFILETIME pftKernelTime,       PFILETIME pftUserTime);

The first parameter is the thread handle to be obtained.

The second parameter returns the number of seconds from the thread creation time to, January 1, January 1, 1601. The Unit is 100ns.

The third parameter indicates the number of seconds from the exit time to on January 1, January 1, 1601. The Unit is 100ns.

The fourth parameter indicates the absolute value of time in the kernel mode of thread execution. The Unit is 100ns.

The fifth represents the absolute value of the time when the thread executes the user-mode code. The Unit is 100ns.

Similarly, GetProcessTime can return the sum of the time of all threads in the process.

The above functions are not enough for high-precision computing. In this case, windows provides the following functions:

BOOL QueryPerformanceFrequency(LARGE_INTEGER *pliFrequency)BOOL  QueryPerformanceCounter(LARGE_INTEGER *pliCount);

These two functions assume that the thread being executed will not be preemptible. They are all for short-lived code blocks. GetCPUFrequencyInMHZ can obtain the cpu frequency.

This article from the "selling cute programmers" blog, please be sure to keep this source http://7677869.blog.51cto.com/7667869/1300631

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.