What core is a given thread running on?

风流意气都作罢 提交于 2019-12-19 04:05:48

问题


Is there a function or any other way to know, programatically, what core of what processor a given thread of my program (pid) is running on? Both OpenMP or Pthreads solutions would help me, if possible. Thanks.


回答1:


This is going to be platform-specific, I would think. On Windows you can use NtGetCurrentProcessorNumber, but this is caveat-ed as possibly disappearing.

I expect this is hard to do, because there's nothing to stop the thread being moved to a new core at any time (in most apps, anyway). As soon as you get the result, it could be out of date.




回答2:


I think on Linux one can try sched_getcpu().




回答3:


For pthreads, I think sched_getaffinity() is at least part of the solution. Not sure exactly how pthreads names the CPU:s and cores, though.




回答4:


This is hard to do portably, as the answer depends both on hardware and OS.

The hardware locality library is a new tool which allows you to query CPU/core/thread etc information (and set affinity bindings) in an OS/hardware agnostic way. It supports a huge list of hardware and OSes, and so should add a lot of portability to these sorts of queries. Once you map out your system's topology, hwloc_get_last_cpu_location will return the CPU the thread last ran on, where CPU can mean core or hardware thread.



来源:https://stackoverflow.com/questions/5819655/what-core-is-a-given-thread-running-on

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