Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What core is a given thread running on?

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.

like image 554
Dervin Thunk Avatar asked Dec 21 '22 14:12

Dervin Thunk


2 Answers

I think on Linux one can try sched_getcpu().

like image 75
Hiuen Avatar answered Jan 12 '23 00:01

Hiuen


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.

like image 31
Steve Townsend Avatar answered Jan 11 '23 22:01

Steve Townsend