Windows core programming Note (5)----thread scheduling, priority

Source: Internet
Author: User
Tags sleep function

1. Operating system thread scheduling processEach thread has a contextual context structure that holds the thread's kernel object, which holds the CPU register at the time of the last execution
The state. At regular intervals, Windows looks at all currently existing thread kernel objects, only some of which are available for scheduling. Windows is available in a scheduled
Thread, and load the data that was last saved in the thread context into the CPU register. (Context Switch)
After the CPU time slices, Windows moves out of the thread, saves the CPU register information to the thread context, switches to another thread, and so on.
2. Thread Suspend and resumeWhen calling CreateProcess or CreateThread, the system creates a thread kernel object and initializes the suspend count to 1 so that the system does not give this line
The CPU is dispatched by the process. After the thread initialization is complete, the function will see if there is a create_suspended flag passed in. If so, the function returns and lets the new thread
Suspend state; No, the function decrements the thread's suspend count to 0, the suspend count is 0 o'clock, and the thread becomes available for dispatch.
Use the ResumeThread function to restore the thread to a scheduled state, which returns the previous suspend count of the thread, and the failure returns 0XFFFFFFFF.
SuspendThread hangs a thread, a thread can hang up to 127 times. SuspendThread is asynchronous, only knowing exactly what the thread is doing, and
Using complete measures to avoid problems or deadlocks caused by suspending threads, calling SuspendThread is safe.
3. SleepThe Sleep function tells the system how much time the thread does not need to dispatch, and the thread automatically discards the time remaining on the CPU time slice. Because Windows is not a real-time operating system
Sleep time is not necessarily accurate.
Sleep (0) tells the system that the current thread is actively abandoning the remaining time of the CPU time slice, forcing the system to call other threads. If you do not have the same or higher-priority
A thread can be dispatched and the system will reschedule the thread.
4. Switch to another threadSwitchToThread switch threads, the system checks to see if there are threads that are in urgent need of CPU. If not, the function returns immediately; If present, SwitchToThread will
When the thread is dispatched, the thread that continues to run can execute a CPU time slice, and then the system dispatches the original thread back.
SwitchToThread and sleep (0)
SwitchToThread allows a low-priority thread to be executed, and Sleep (0) is not dispatched even with a starvation with a low priority.
5. Thread Context StructureThe system uses the context to record the state of a thread so that it can continue execution from the context stop the next time it executes.
In all of the structures defined by Windows, the context is the only CPU-specific.
You can use GetThreadContext to get the context of the thread kernel, you need to suspend the thread before use, otherwise the context information obtained is related to the current running thread's
The context is inconsistent. A thread has two context modes: User mode and kernel mode, and GetThreadContext gets the contextual information of the user mode.
6.

Higher-priority threads always preempt lower-priority threads, regardless of whether the low-priority thread is running. When the system starts, a page clear 0 thread is created (

Zero page thread), which has a priority of 0. Page 0 Thread is responsible for all unused pages in system memory when no other thread needs to execute
The face is zeroed.
7, scheduling i\o request priority levelIf a low-priority thread gets CPU time, it is easy to into row hundreds of i\o requests in a short period of time, i\o requests typically take time to process, as
This low-priority thread may suspend high-priority threads so that they cannot complete the task, which significantly affects the responsiveness of the system.
The Thread_mode_background_begin property can be passed through setthreadpriority, telling the Windows thread that a low-priority i\o request should be sent, which
The CPU scheduling priority of the thread is also reduced. You can restore the priority of a thread by setting the Thread_mode_background_end property.
The system does not allow a thread to change the i\o request priority of another thread.
Similarly, the Process_mode_background_begin property can be passed through Setpriorityclass, setting all threads in the process to a low-priority i\o
Requests and CPU scheduling. The Process_mode_background_end property to restore.

The system does not allow a process to change the i\o request priority of another process.

Test code

Suspend all threads in a process, use note (may have new threads created and destroyed after traversal)
void Suspendprocess (DWORD dwpid, BOOL bsuspend) {HANDLE hsnapshort = CreateToolhelp32Snapshot (Th32cs_snapthread, DwPid) ; if (invalid_handle_value! = hsnapshort) {THREADENTRY32 te = {sizeof (THREADENTRY32)}; BOOL BRet = Thread32first (Hsnapshort, &te), while (BRet) {if (Te.th32ownerprocessid = dwpid) {HANDLE hthread = Openth Read (Thread_suspend_resume, FALSE, Te.th32threadid); if (NULL = = hthread) continue;if (bsuspend) suspendthread (HThread) ; Elseresumethread (Hthread); CloseHandle (hthread);} BRet = Thread32next (Hsnapshort, &te);} CloseHandle (Hsnapshort);}}
Set thread priority, get thread context information

HANDLE hthread = CreateThread (null, 0, testthread, 0, create_suspended, NULL); SetThreadPriority (Hthread, thread_priority_idle| Thread_mode_background_begin); ResumeThread (Hthread); CloseHandle (Hthread); SuspendThread (Hthread); SuspendThread (hthread);D Word dwcount = ResumeThread (hthread); WaitForSingleObject (Hthread, INFINITE); CloseHandle (Hthread); Suspendprocess ((), TRUE); Suspendprocess (A, FALSE); Context Context;memset (&context, 0, sizeof (context)); Contextflags = Context_integer;//context_control; SuspendThread (GetCurrentThread ()); GetThreadContext (GetCurrentThread (), &context);



Windows core programming Note (5)----thread scheduling, priority

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.