3

I have 2 threads. I want to tell one of them to run on first cpu and the second one on the second cpu for example in a machine with two cpu. how can I do that?

this is my code

UCI UCIMain = new UCI(); Thread UCIThread = new Thread(new ThreadStart(UCIMain.main)); UCIThread.Priority = ThreadPriority.BelowNormal; UCIThread.Start(); 

and for sure the class UCI has a member function named main. I want to set this thread in 1st processor for example

13
  • 4
    What is the reason for caring? (..not trying to be sarcastic) Commented Sep 14, 2012 at 15:11
  • There are tools like the Task Parallel Library to optimize these concerns for us - why reinvent the wheel? Commented Sep 14, 2012 at 15:12
  • 2
    It's probably best just to let the OS decide that. Commented Sep 14, 2012 at 15:12
  • 2
    it is obvious it only uses one processor - then it is obvious that it only can use 1 CPU for some other reason. You are solving the wrong problem. Commented Sep 14, 2012 at 15:32
  • 2
    If the usage is 50% CPU, then I'm betting that Henk is 100% right - only one thread is running. If both threads were ready all the time, the OS would surely run one on each core. Commented Sep 14, 2012 at 18:25

3 Answers 3

5

I wouldn't recommend this, but if you really need to: it is possible by tapping into the native Win32 system calls, specifically SetThreadAffinityMask. You will need to do some DllImports:

[DllImport("kernel32.dll")] static extern IntPtr GetCurrentThread(); [DllImport("kernel32.dll")] static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask); 

And then use the them inside each spawned thread (with a different parameter for the mask, of course):

// set affinity of current thread to the given cpuID SetThreadAffinityMask(GetCurrentThread(), new IntPtr(1)); // CPU 0 
Sign up to request clarification or add additional context in comments.

Comments

2

On .NET, you have ProcessThread.ProcessorAffinity and ProcessThread.IdealProcessor

But since you are talking about a Thread and not Process, I believe there is no directly available way of doing this in .NET.

There is Thread.SetProcessorAffinity() but it's only available for XNA on Xbox.

3 Comments

see my edit, there is not an out-of-the-box solution for Threads.
Yeah - you might want to edit this answer since my answer was deleted by @casperOne - a supposed moderator...!
thanks! just did. if yours was deleted mine should've been too :P
2

Don't do this. OS task scheduler is much more clever than manual tweaks. You can technically use thread affinity, but it's not usually a good idea. Instead use thread pool or TPL library.

If one core is super busy and another one is not. Then one thread will be starving for processing power, whereas another core will not be loaded at all.

2 Comments

the problem is my program only use 50% of the cpu. it is obvious it only uses one processor
Can you describe what these two threads do? Can you post some simplified code? Are they doing any IO?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.