How to explicitly assign a thread to a core of the processor?

Greetings, everyone!

I have a question, maybe simple and stupid since I've never touched this area before.

I have a 64-bit laptop with Windows 7 Professional and a 8-core CPU. I use Visual Studio 2012 Express to build my program.

I am coding a project which is highly required at the speed performance, thus I need everything I can grab to speed it up. What I want to achieve is to explicitly assign my threads to each of the CPU cores.

Therefore, I have few questions as follows:

1. Is it necessary to explicitly assign threads to cores?

2. I know Windows has a well designed method to automatically allocate the threads to the CPU cores. But what I don't know is if this is efficient? Because sometimes I monitor the CPU usage, I find it usually in idle status even I am running something.

3. If, for a specific task, explicitly allocate threads to the cores is more efficient than leave it to the operating system. How to achieve so? Is there any examples or code clips that I can learn from?

Thank you very much in advacne. I would extremely appreciate if you can bring me some ideas.

I can't answer your specific questions, but...

My inderstanding is that Windows thread scheduling in good for general purposes, but I have come across customing scheduling code for special purposes. But this is something I'd only expect to see on a dedicated server.

As well as adjusting the thread priority, as the article mordoran pointed you at shows, you can control where your threads runs using SetThreadAffinityMask

SetThreadAffinityMask function

BUT this function is seen as rather problematic, even dangerous, when mis-used.

You mention tasks in your final point which, together with you general question, suggests you might be interested in Microsoft PPL (Parallel Patterns Library) or Intel TBB (Threading Building Blocks). I don't know a lot about these yet, but am currently trying to learn more about them.

In the end, I think that trying trying to explicitly allocate threads to the cores would probably be rather tricky. But these new task-based approaches might be able to help.


Parallel Patterns Library (PPL)

Threading Building Blocks
Last edited on
Topic archived. No new replies allowed.