Determine on which physical processor my code is currently running

杀马特。学长 韩版系。学妹 提交于 2019-12-10 19:10:37

问题


Is there a Windows API or any way to determine on which physical processor/core my current thread runs? I don't need that information. I'm just curious.

I'm not interested in the processors the thread is allowed to run on. I would like to know on exactly which one it currently runs. I know that the threads switch pretty fast from one to another...


回答1:


Threads will often switch from processor to processor, so it's kind of meaningless, but you can use GetCurrentProcessorNumber.

As others have said, you can use GetProcessAffinityMask or GetThreadIdealProcessor, but those will only work if you've already set an affinity mask or ideal processor for the thread.




回答2:


You can query the processor affinity with GetProcessAffinityMask. If you haven't set processor affinity, I'm unsure how useful the result will be though, as the thread can flit between processors.




回答3:


For controlling which processor your process or thread runs on using the Windows API you can use SetThreadAffinityMask or SetProcessAffinityMask.

These work by setting bits in a bit mask, where each bit represents a processor your thread or process can be scheduled for:

BOOL WINAPI SetProcessAffinityMask(
  __in  HANDLE hProcess,
  __in  DWORD_PTR dwProcessAffinityMask
);

Call GetProcessAffinityMask to discover which processors are available for use in these calls.



来源:https://stackoverflow.com/questions/1341400/determine-on-which-physical-processor-my-code-is-currently-running

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