std::thread不提供获取当前线程的系统id的方法,仅可以获取当前的线程id,但是我们可以通过建立索引表的方式来实现
std::mutex m;
std::map<std::thread::id, pid_t> threads;
void add_tid_mapping()
{
std::lock_guard<std::mutex> l(m);
threads[std::this_thread::get_id()] = syscall(SYS_gettid);
}
void wrap(void (*f)())
{
add_tid_mapping();
f();
}
而后用其创建线程
std::thread t1(&wrap, &SayHello);
然后用如下方式获取线程id
pid_t tid = ;
while (tid == )
{
std::lock_guard<std::mutex> l(m);
if (threads.count(t1.get_id()))
tid = threads[t1.get_id()];
}
转自:https://*.com/questions/15708983/how-can-you-get-the-linux-thread-id-of-a-stdthread