What core is a given thread running on?

后端 未结 4 767
面向向阳花
面向向阳花 2021-01-06 10:21

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 solution

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 10:47

    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.

提交回复
热议问题