WINDOWS core programming learning experience-thread context, priority and relevance

Source: Internet
Author: User

The CONTEXT structure plays an important role in thread scheduling. In all WINDOWS-defined structures, CONTEXT is unique dependent on the CPU. We can call GetThreadContext to obtain the status of the current CPU register.

BOOL GetThreadContext(       HANDLE pThread,       PCONTEXT pContext);

Here there are two parameters: pThread is the input, that is, the thread handle to obtain the CPU status, and the second parameter is the CONTEXT structure pointer. After the CONTEXT structure is allocated, You need to initialize its ContextFlag flag, status registers to be obtained. ContextFlag can be

CONTEXT_CONTROL indicates the control register

CONTEXT_INTERER indicates the integer register

CONTEXT_FLOAT indicates a floating-point register.

CONTEXT_ALL indicates all registers

Before calling GetThreadContext, you need to call SuspendThread to suspend the thread. Note that this GetThreadContext can only obtain the user mode context. When a thread is suspended, the thread is running in kernel mode, the thread will continue to run and will be suspended only when the user mode is returned. We can not only obtain the thread context, but also set it.

BOOL SetThreadContext()       HANDLE hThread,       CONST CONTEXT *pContext);

The process priority is described below.

When waiting for the CPU, the ready thread allocates the CPU according to the priority. Therefore, the priority is closely related to thread scheduling.

In Windows, the thread priority ranges from 0 to 31. Each thread is assigned a priority. When the system determines the thread to which the cpu is allocated, it first checks the thread with priority 31 until all threads with priority 31 are scheduled. Then check the next priority thread. As long as there is a thread with a priority of 31, the system will not schedule threads of the 0-30 level. Low-priority threads cannot get the cpu time for a long time, which is called hunger. This does not happen frequently, because most threads are not schedulable.

When the system starts, an idle thread with a priority of 0 is created. The whole system only has a priority of 0. It clears all idle pages in the system memory from 0 when no other threads are running in the system.

The priority of a thread is determined by adding the priority of the process to which the thread belongs. This value is called the basic priority of the thread.

WINDOWS supports six priority classes:

Idle, below normal, normal, above normal, high, and real-time. Of course, normal is the most common priority class, which is used by 99% of applications.

WINDOWS supports 7 relative thread priorities

Idle, lowest, below normal, normal, abve normal, highest, and time-critical. These priorities are relative to process priorities. Most threads use normal priority.

When creating a process, we can change the priority of the process by changing the value of the fdwCreate parameter.


After a process is running, you can use SetPrioritClass to set the priority class of the process.

BOOL SetPriorityClass(         HANDLE hProcess,         DWORD fdwPriority);

You can call DWORD GetPriorityClass (HANDLE hProcess); to obtain the priority class of the process.

The above is the specified process priority class. When CreateThread is called to create a thread, its thread priority is always set to normal. You can call the following function to change the thread priority:

BOOL SetThreadPriority(        HANDLE hThread,    int nPriority);

NPriority can be the following identifier:



Above-normal thread_priority_abve_normal





You can call int GetThreadPriority (HANDLE hThread) to return the relative priority of the thread.

Windows does not return the thread priority function, but provides the return process priority class and the relative thread priority.

In some cases, the system also increases the priority of a thread. For example, a thread is waiting for the user to press a message. When you press a key, the system will put a WM_KEYDOWN message in the thread message queue. In this case, the thread becomes schedulable. The keyboard device driver will temporarily increase the thread priority. After the time slice ends, the system will reduce the priority of the thread by one, and the execution of the third time slice minus one. Until the basic priority is maintained.

Note: The current thread priority is not lower than the basic process priority. In addition, the device driver can determine the range of dynamic increase. The system only increases the priority value from 1 ~ 15 threads. This range is called a dynamic priority range. You can call the following function to disable the system from dynamically Improving the thread priority:

BOOL SetProcessPriorityBoost(       HANDLE hProcess,       BOOL bDisablePriorityBoost);

This function does not dynamically increase the priority of all threads in this process.

BOOL SetThreadPriorityBoost(       HANDLE hThread,       BOOL bDisablePriorityBoost);

This function does not dynamically increase the priority of a thread.

There is also a situation where priority is dynamically increased: When hunger is detected, that is, a thread cannot be scheduled for a long time due to its low priority. The system will dynamically increase the priority of this thread. The system allows it to run two time slices. After the two time slice ends, the basic priority is immediately restored.

The window in use is called the foreground window. This process is called a foreground process. To improve the responsiveness of the foreground process, windows will fine-tune the Scheduling Algorithm for the threads in the foreground process. Is the ratio of threads in the foreground process. Generally, more time slices are used.


By default, windows uses soft association when allocating cpu resources. That is to say, when other factors are the same, the system enables the thread to run on the last running processor. This helps reuse data that is still in the processor cache.

The system determines the number of CPUs at startup. The application can call GetSysInfo to query the number of CPUs. To restrict all threads of a process to run on some CPUs, you can call:

BOOL SetProcessAffinityMask(         HANDLE hProcess,         DWORD_PTR dwProcessAffinityMask);

The first parameter indicates the Process Handle to be set.

The second parameter is a bitmask. Indicates the cpu on which the thread can run.

Note that the child process inherits the associativity of the parent process.

GetProcessAffinityMask returns the associated mask of the process.

You can also set a thread to run only on a group of CPUs:


Sometimes it is not a good idea to force a thread to run only on a specific cpu. Windows allows a thread to run on a cpu, but if needed, it will be moved to an idle cpu.

To set an ideal cpu for a thread, you can call:

DWORD SetThreadIdealProcessro(      HANDLE hThread      DWORD dwIdealProcessor);

DwIdealProcessor is an integer between 0 and 31/63. Indicates the cpu that the thread wants to set. You can pass in the MAXIMUM_PROCESSOR value, indicating that there is no ideal cpu.

This article from the "selling cute programmers" blog, please be sure to keep this source

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: 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.