Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10965

How to start a thread on a specific core?

I have a quad core CPU, and lets say I always want to start a Thread on the second core.

Is that possible in C#?

Upvotes: 8

Views: 14789

Answers (3)

Timeless
Timeless

Reputation: 7547

Set ProcessorAffinity of the process:

0x0001 = 0000 0001 - run on 1st core
                 ↑
0x0002 = 0000 0010 - run on 2nd core
                ↑
0x0003 = 0000 0011 - run on 1st and 2nd core
                ↑↑
0x0004 = 0000 0100 - run on 3rd core
               ↑

Simple code:

using (var process = Process.GetCurrentProcess())
{
  // only run on core number 1
  process.ProcessorAffinity = (IntPtr) 0x0001;
}

Upvotes: 2

Kynth
Kynth

Reputation: 2607

Yes. Check out ProcessorAffinity for Windows or SetProcessorAffinity for XBox XNA.

This is also discussed on another Stackoverflow question.

Upvotes: 9

Chris Van Opstal
Chris Van Opstal

Reputation: 37587

Yes, take a look at the ProcessorAffinity property for the thread.

Upvotes: 8

Related Questions