Study Notes in Windows via C/C ++-thread relevance

Source: Internet
Author: User

By default, Windows Vista (more than 2000 systems) uses "soft affinity" (soft-related) operations to allocate threads to the CPU. This means that if other factors remain unchanged, the system tries to run the thread on the CPU where the thread was last run, so that data is retained in the cache of the CPU.

Hard affiinty is relative to soft affinity, which can be used to control which CPU runs and which threads.

During system boot, the system determines that several CPUs in the computer can be used. In an application, call the getsysteminfo function to obtain the number of CPUs.

Generally, a thread can run on any CPU. Of course, you can use the "soft affinity" mechanism that comes with windows to allow windows to automatically allocate CPU to a thread.

Maybe you want to have some restrictions on the CPU allocated to the thread, and use the "hard affinity" mechanism.

The setprocessaffinitymask function limits that a specific process can only run on a subset of the CPU.

 

Bool setprocessaffinitymask (
Handle hprocess,
Dword_ptr dwprocessaffinitymask );

 

The first parameter of this function specifies the process to be restricted, and the second parameter is a "bit shielded" data. Each bit in it represents a CPU code (starting from 0 ), for example, 0x00000001 indicates that CPU 0 is selected, that is, "threads in the process" can only run on CPU 0; 0x00000005 indicates that both CPU 0 and CPU 2 are selected.

A sub-process in a process can inherit the relevance of the process, or use the job kernel object to limit certain processes to a set of CPUs.

 

You can call the getprocessaffinitymask function to obtain a process correlation shielding information.

 

Bool getprocessaffinitymask (
Handle hprocess,
Pdword_ptr pdwprocessaffinitymask,
Pdword_ptr pdwsystemaffinitymask );

This function uses the second parameter to return CPU relevance information of the specified process, and the third parameter to return system relevance information. System relevance indicates which CPUs of the system can process threads, and process relevance is always a subset of system relevance.

 

We discussed how to restrict all threads in a process to a group of CPUs. Sometimes you want to restrict a specific thread of a process to a group of CPUs.

The setthreadaffinitymask function restricts that a thread can only run on a group of CPUs.

Dword_ptr setthreadaffinitymask (
Handle hthread,
Dword_ptr dwthreadaffinitymask );

 

The second parameter of the function has the same meaning as the second parameter in the setprocessaffinitymask function. A correct CPU subset must also be specified, and the specified thread can only run on this CPU subset. This function returns information about the original thread.

For example, if there are four threads and four available CPUs, you want thread 1 to exclusively occupy CPU 0, the other three threads can only run on CPU 1, CPU 2, and CPU 3, which can be encoded as follows:

Setthreadaffinitymask (hthread0, 0x00000001 );
Setthreadaffinitymask (hthread1, 0x0000000e );
Setthreadaffinitymask (hthread2, 0x0000000e );
Setthreadaffinitymask (hthread3, 0x0000000e );

It is often inappropriate to use the hard affinity mechanism to forcibly limit a thread to run on a group of CPUs. This will reduce the CPU usage.

Allocate an ideal CPU to a thread. The setthreadidealprocessor function can do this:

DWORD setthreadidealprocessor (
Handle hthread,
DWORD dwidealprocessor );

The second parameter of this function is not a bit blocking data, but a 0 ~ 31 (32-bit system) or 0 ~ The integer of 63 (64-bit system. This data indicates the preferred CPU. You can also pass maximum_processors to indicate that there is no ideal CPU.

 

You can process relevance at the beginning of a program. The code is similar to the following code:

// Load an EXE file Image
Ploaded_image ploadedimage = imageload (szexename, null );

// Obtain the configuration information of the loaded EXE file

Image_load_config_directory ilcd;
Getimageconfiginformation (ploadedimage, & ilcd );

// Change the process's affinity

Ilcd. processaffinitymask = 0x00000003; // I desire CPUs 0 and 1

// Save the new loading information (including the affinity set just now)

Setimageconfiginformation (ploadedimage, & ilcd );
Imageunload (ploadedimage); // detaches an image from the memory

 

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.