How to start a thread on a specific core?

放肆的年华 提交于 2019-12-06 04:17:14

问题


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


回答1:


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

This is also discussed on another Stackoverflow question.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/6584397/how-to-start-a-thread-on-a-specific-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!