[英]How can you get the Linux thread Id of a std::thread()
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
). 我正在玩std::thread
,我想知道如何获得一个新的std::thread()
的线程id,我不是在讨论std::thread::id
而是提供给我的操作系统ID线程(您可以使用pstree
查看它)。 This is only for my knowledge, and it's targeted only to Linux platforms (no need to be portable). 这仅限于我的知识,它仅针对Linux平台(无需可移植)。
I can get the Linux Thread Id within the thread like this : 我可以像这样在线程中获取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;
}
But how can I retrieve the same id within the main loop ? 但是如何在主循环中检索相同的id? I did not find a way using std::thread::native_handle
. 我没有找到使用std::thread::native_handle
。 I believed it was possible to get it trough pid_t gettid(void);
我相信它有可能通过pid_t gettid(void);
得到它pid_t gettid(void);
since the c++11 implementation relies on pthreads, but i must be wrong. 因为c ++ 11实现依赖于pthreads,但我一定是错的。
Any advices ? 有什么建议吗? Thank you. 谢谢。
Assuming you're using GCC standard library, std::thread::native_handle()
returns the pthread_t
thread ID returned by pthread_self()
, not the OS thread ID returned by gettid()
. 假设你正在使用GCC标准库std::thread::native_handle()
返回pthread_t
通过返回的线程ID pthread_self()
而不是返回操作系统的线程ID gettid()
std::thread::id()
is a wrapper around that same pthread_t
, and GCC's std::thread
doesn't provide any way to get the OS thread ID, but you could create your own mapping: std::thread::id()
是同一个pthread_t
的包装器,而GCC的std::thread
没有提供任何获取操作系统线程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();
}
Then create your thread with: 然后创建你的线程:
std::thread t1(&wrap, &SayHello);
then get the ID with something like: 然后通过以下方式获取ID:
pid_t tid = 0;
while (tid == 0)
{
std::lock_guard<std::mutex> l(m);
if (threads.count(t1.get_id()))
tid = threads[t1.get_id()];
}
Some pthread implementations , eg Android 21 +, provide 一些pthread 实现 ,例如Android 21 +,提供
pid_t pthread_gettid_np(pthread_t);
The implementation may use the internal structure of struct pthread_t
to retrieve the native thread id, same as the one returned by gettid()
or syscall(SYS_gettid)
when called in the context of that thread. 实现可以使用struct pthread_t
的内部结构来检索本机线程id,与在该线程的上下文中syscall(SYS_gettid)
时由gettid()
或syscall(SYS_gettid)
返回的相同。
How about this: 这个怎么样:
pid_t gettid (void)
{
return syscall(__NR_gettid);
}
http://yusufonlinux.blogspot.com/2010/11/get-thread-id-from-linux.html http://yusufonlinux.blogspot.com/2010/11/get-thread-id-from-linux.html
Looks like __NR_gettid is defined in unistd.h 看起来__NR_gettid在unistd.h中定义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.