Windows via C/C ++ Study Notes-Thread Scheduling

Source: Internet
Author: User

A preemptible operating system (such as Microsoft Windows) must use an algorithm to determine how long the thread should be scheduled.

The thread Kernel Object of each thread maintains a context structure, which stores the register information and status of the thread last executed by the CPU. For every 20 ms, Windows can view all existing thread kernel objects. Only some of these objects can be scheduled. In Windows, select a scheduling thread Kernel Object and load the register information stored in the Context Structure of the kernel object. This process is called "contest switch ". Windows actually saves a record that records the number of times each thread is executed.

Then, the thread starts to execute code and process data. The code and data exist in the process address space of the thread. About 20 ms later, Windows saves the CPU register information to the Context Structure of the thread, and the thread stops running. The system checks all the schedulable thread kernel objects again, selects another thread kernel object, loads its context structure, and then continues ......

Note: The exact time of thread running cannot be obtained. This is a real-time operating system function, while Windows is a preemptible operating system.

In the system, many threads are in the "suspended" state, because they need to wait for one or more specific events to change from the suspended state to the schedulable state.

 

There is a "value" inside the thread kernel object that specifies the "suspension count" of the current thread ". When the CreateProcess and createthread functions are called to create a thread, the thread kernel object is created, and the "pending count" is initialized to 1 to prevent the newly created thread from being scheduled, because initialization may not be fully done yet. After the thread is fully initialized, the CreateProcess and createthread functions check whether you have passed the create_suincluded flag ). If this flag is passed, the function returns and the new thread enters the "suspended" state. If the flag is not passed, the function will decrease the "pending count" to 0. When the "suspend count" of a thread is 0, it indicates that it can be scheduled (unless it is waiting for some events or other objects, such as keyboard input ).

Creating a "suspended" thread means that you can change the environment (such as priority) of the thread before it is executed ). You can use the resumethread function to change the thread to A schedulable state.

DWORD resumethread (handle hthread );

If the function is successful, the previous suspension count of the thread is returned. Otherwise,-1 (0 xffffffff) is returned ).

A thread can be suspended for multiple times. If a thread is suspended for three times, you need to call the resumethread function to wake it up three times. resumethread will "decrease" the "suspend count" of the specified thread ".

To suspend a thread, you only need to call the suspendthread function and pass a thread handle to it.

DWORD suspendthread (handle hthread );

One thread can call this function to suspend another thread. This function also returns the previous "suspended count" of the thread ". Note that the suspendthread function is asynchronous in kernel mode. When the thread is suspended, the execution in user mode will not occur.

 

To make a thread sleep for a period of time, call the sleep function.

Void sleep (DWORD dwmilliseconds );

After running this function, the thread will not be scheduled again within a specified time. You can pass the infinite function to sleep the thread endlessly. You can also pass a normal positive number to let the thread sleep for a certain period of time (in milliseconds ). It can also be passed 0, which means that the thread abandons the current time slice and forces the system to schedule another thread. However, the system can schedule this thread at any time, that is, this thread does not sleep.

 

The system provides a function that allows scheduling another thread. This function is:

Bool switchtothread ();

Call this function to check whether there are "Hunger" Threads. If so, the function immediately schedules the hunger thread, which may have a low priority.

If a thread can be scheduled exists, the function returns true; otherwise, false.

The difference between this function and calling sleep (0) is that this function allows low-priority threads to be scheduled.

 

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.