How to start a thread on a specific core?
Asked Answered
M

3

8

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#?

Mikesell answered 5/7, 2011 at 14:33 Comment(3)
See How Can I Set Processor Affinity in .NET?.Caesarism
Check these discussions: social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/… eggheadcafe.com/community/aspnet/2/10156145/… gamedev.net/topic/…Pilpul
Possible duplicate of How Can I Set Processor Affinity in .NET?Zoonosis
P
9

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

This is also discussed on another Stackoverflow question.

Petiolate answered 5/7, 2011 at 14:39 Comment(0)
W
8

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

Wnw answered 5/7, 2011 at 14:35 Comment(0)
W
2

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;
}
Wholesale answered 25/8, 2017 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.