Windows binds threads (processes) to the specified CPU core

Source: Internet
Author: User

A program specifies that running on a single CPU is faster than not specifying the CPU runtime. There are two main reasons for this:
1) performance of CPU switching loss.
2) Intel's automatic frequency reduction technology and Windows mechanism conflict: Windows has a function to balance the load, you can assign a thread at different times to different CPUs, so that each CPU is not "too tired." However, Inter has another technology called SpeedStep, when a CPU does not run full load automatically reduce frequency to achieve the purpose of energy saving and emission reduction. These two features are actually conflicting: A program is assigned to multiple CPUs, each CPU is not fully loaded--each CPU will be down frequency->windows the performance of each CPU is reduced, and the program execution speed is reduced.

Therefore, it is necessary to bind a thread (process) to a specified CPU core so that Windows does not take the liberty of helping us to distract the task, thereby increasing single-threaded efficiency. There are two ways to implement a binding process to a specified CPU:
1) Manual adjustment: In the process of the resource manager, set the correlation, you can set the process to some or some specified CPU core.

This method is the simplest and most efficient, as you can choose based on the current CPU load.
2) Automatic Code adjustment:
Reference: http://www.cnblogs.com/kex1n/archive/2011/05/09/2040924.html
The specific functions are:

1 Dword_ptr Setthreadaffinitymask (HANDLE hthread, dword_ptr dwthreadaffinitymask);

Where the first parameter is a thread handle and the second parameter is a mask.
If you want to know the handle of the current thread, you can get it through the function: GetCurrentThread (). Otherwise, when creating multithreading, you can also get a handle to the thread you created.
The second parameter is mask, with a value of 0~2^31 (32-bit) and 0~2^63 (64-bit), each of which represents whether each CPU is used.
For example, if you want to specify a process to the No. 0 CPU, then mask=0x01
1th One cpu:mask=0x02
2nd cpu:mask=0x04 (note not 0x03)
3rd One cpu:mask=0x08
And so on
If you want to specify multiple CPUs:
For example, No. 0, 1: mask=0x03
1th, 2: mask=0x06
And so on
If the number of CPUs is insufficient, a modulo operation is performed. For example, a total of 4 CPUs, then mask=0x0010 and 0x01 the same.
The advantage of this method is that multi-threading does not have to manually select the CPU each time, the disadvantage is that in the event of a high CPU load, then the program execution speed is slow (think alike so everyone grab the same CPU go ~ ~)
The effect is as follows:

There is also a practical function to get the number of cores for the current CPU:

1
2
3
System_info INFO;
GetSystemInfo (&info);
printf ("Number of processors:%d.\n", info.dwnumberofprocessors);

The output is the number of logical cores, such as the i3 processor is a dual core four thread, output 4. The i5 processor is a quad core four thread and the output is also 4.
This makes it easy to know how many CPUs are in the current system, and also facilitates the selection of threads.

Transferred from: http://hymike.net/blog/?p=460

Windows binds threads (processes) to the specified CPU core

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.