开发者

How to start a thread on a specific core?

开发者 https://www.devze.com 2023-03-17 23:11 出处:网络
I have a quad core CPU, and lets say I always开发者_JAVA百科 want to start a Thread on the second core.

I have a quad core CPU, and lets say I always开发者_JAVA百科 want to start a Thread on the second core.

Is that possible in C#?


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

This is also discussed on another Stackoverflow question.


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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号