Is there a sane way to map a pthread_t
value (as returned from pthread_create()
or std::thread::native_hanle()
) to pid(tid) in Linux? Before someone gets duplicate-happy, this is not about finding thread's own pid (which can be done with gettid()
).
The insane way would be to somehow compel a thread to call gettid()
and pass along the result, but that's way too much trouble.
One of the possible applications I have in mind is to reconcile threads created within program (where pthread_t
is available) with output provided by ps -T
.
pthread_t
is a pointer tostruct pthread
, which has atid
member. But I don't know any acceptable way to read this information. If you have debug symbols available, you can check this struct out in gdb withptype pthread
. For debug purposes only, you can read the offset oftid
withp &((pthread*)0)->tid
, then use this offset in the program itself to readtid
frompthread_t
. Or you can use read the offset from the symbol file itself with some dwarf reader library, to be "portable". – Teengetpid
should return it. posix threads might not, sogetpid
will return pid of the main process. check this: https://mcmap.net/q/828878/-understanding-pthreads – Stokehole