小编Cha*_*ble的帖子

你怎么能得到一个std :: thread()的Linux线程ID

我正在玩,std::thread我想知道如何获得一个新的线程ID std::thread(),我不是在讨论,std::thread::id而是提供给线程的操作系统ID(您可以使用它查看pstree).这仅限于我的知识,它仅针对Linux平台(无需可移植).

我可以像这样在线程中获取Linux Thread Id:

#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;
}
Run Code Online (Sandbox Code Playgroud)

但是如何在主循环中检索相同的id?我找不到使用的方法 std::thread::native_handle.我认为有可能pid_t gettid(void);通过c ++ 11实现依赖于pthreads来实现它,但我一定是错的.

有什么建议吗?谢谢.

c++ multithreading pthreads c++11

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

pthreads ×1