I was playing with std::thread
and I was wondering how is it possible to get the thread id of a new std::thread()
, I am not talking about std::thread::id
but rather the OS Id given to the thread ( you can view it using pstree
).
This is only for my knowledge, and it's targeted only to Linux platforms (no need to be portable).
I can get the Linux Thread Id within the thread like this :
#include <iostream>
#include <thread>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
void SayHello()
{
std::cout << "Hello ! my id is " << (long int)syscall(SYS_gettid) << std::endl;
}
int main (int argc, char *argv[])
{
std::thread t1(&SayHello);
t1.join();
return 0;
}
But how can I retrieve the same id within the main loop ? I did not find a way using std::thread::native_handle
. I believed it was possible to get it trough pid_t gettid(void);
since the c++11 implementation relies on pthreads, but i must be wrong.
Any advices ? Thank you.