Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

Chosing CPU for thread programatically

Hello, I have a following question:
I'm going to write a program where one thread is busy on data aquisition from PCI card, and other threads are doing all other things. Now, I would like to assign that aquisition thread to one CPU, and other threads to the other CPU (It may be really two CPU's, or just hyper-threaded). How do I choose CPU for thread programatically in e.g. C++? Is it possible, or will it work as assigning CPU according to how much one is busy, so the other will be then assigned?

thank you very much for answering to this question
Peter Sliwka
0 Kudos
4 Replies
You check out SetThreadAffinityMask() from MS Platform SDK.
There are very limited number of cases when it is beneficial to use. In most cases it is better to let Windows scheduler assigningCPUs (or cores) forevery thread for every time slice. Operating system cares about the whole system functioning and takes into account every system parameter (number of CPUs, cores/hipersthreadingof CPUs, shared or split cache) while an application (programmer) cares about it'sparticular function only.
When you have to make sure that the CPU core is the same for every instruction of your code, you use thread affinity.
Thank you, that was very helpful for me :)
Not applicable

I've done some testing via Managed C# code with Thread Affinity and noticed a performance decline when tasking a single HT/Multi-core processor by thread as opposed to usingThreadPool Queue.

Im far (quite literally in outter space) from being an expert in this area but heck I might as well throw my comments out and see what other have to say.. I know I have quite a bit of learning to do anyway..

Kevin (Intel ASP.Net Web dude...)

Black Belt

From your description "single HT/Multi-core processor" Windows will percieve 4 processors when in fact you have 2 cores, each with two HT thread capability. What you want is one core dedicated to your applicationi (both HT threads on that core reserved but only one in use).

I think your best results (for the priorityapplication thread) would be to isolate each core. With "processors" 0 and 1 on one core and "processors" 2 and 3 on the second core. Using the Task Manager | Processes tab | right-click on each ofnot-your-application processies and set affinity to restrict processing to the 1st core (cpu0 and cpu1). Then modify your application to set affinity to CPU2 (2nd core).

If you start any applications after initialization (Task Manager setting of affinity) then you will have to reset affinity on the new processes.

Experiment running this way and check results.

Jim Dempsey