Linux 如何从任意 pthread_t 获取线程 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/558469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a thread ID from an arbitrary pthread_t?
提问by Skrud
I have a pthread_t, and I'd like to change its CPU affinity. The problem is that I'm using glibc 2.3.2, which doesn't have pthread_setaffinity_np(). That's OK, though, because pthread_setaffinity_np() is itself a wrapper of sched_setaffinity(), which can be called by passing a thread ID instead of a process ID to set the affinity for an arbitrary thread.
我有一个 pthread_t,我想改变它的 CPU 亲和性。问题是我使用的是 glibc 2.3.2,它没有pthread_setaffinity_np()。不过没关系,因为 pthread_setaffinity_np() 本身就是sched_setaffinity()的包装器,可以通过传递线程 ID 而不是进程 ID 来调用它来设置任意线程的亲和性。
BUT... The thread id that sched_setaffinity can work with is an OS thread id, the kind that you can get from the gettid()system call. This is different from the opaque type pthread_t, and gettid() will only return the thread-id of the current thread. I need to be able to set the CPU affinity of an arbitrary thread.
但是... sched_setaffinity 可以使用的线程 id 是操作系统线程 id,您可以从gettid()系统调用中获得的那种。这与不透明类型 pthread_t 不同,gettid() 只会返回当前线程的线程 ID 。我需要能够设置任意线程的 CPU 亲和性。
Unfortunately, I can't access the pthread's private parts, which would let me steal the thread id by casting a pthread_t to a struct pthread *
. All the better, I guess, since relying on private implementations is asking for even moretrouble.
不幸的是,我无法访问 pthread 的私有部分,这会让我通过将 pthread_t 强制转换为struct pthread *
. 我猜这更好,因为依赖私有实现会带来更多麻烦。
I've also been reading up on the pthread_getunique_npfunction, however this returns a "unique integral identifier" -- which I don't believe is in any way shape or form equivalent to an OS thread id.
我也一直在阅读pthread_getunique_np函数,但是这会返回一个“唯一的整数标识符”——我不相信它在任何方面都与操作系统线程 ID 等效。
Hence, the question: How can I get a thread ID from an arbitrary pthread_t?
因此,问题是:如何从任意 pthread_t 获取线程 ID?
采纳答案by Tom Alsberg
Since pthread
s do not need to be implemented with Linux threads (or kernel threads at all, for that matter), and some implementations are entirely user-level or mixed, the pthread
s interface does not provide functions to access these implementation details, as those would not be portable (even across pthread
s implementations on Linux). Thread libraries that use those could provide this as an extension, but there do not seem to be any that do.
由于pthread
s 不需要用 Linux 线程(或者根本不需要内核线程)来实现,并且一些实现完全是用户级的或混合的,pthread
s 接口不提供访问这些实现细节的函数,因为那些会不可移植(即使跨pthread
Linux上的s 实现)。使用这些的线程库可以将其作为扩展提供,但似乎没有。
Other than accessing internal data structures of the threading library (which you understandably do not want, although with your assumptions about processor affinity and Linux thread IDs, your code will not be portable anyway), you may be able to play a trick at creation time, if you control the code that creates the threads:
除了访问线程库的内部数据结构(您可以理解不希望这样做,尽管您对处理器关联性和 Linux 线程 ID 的假设,您的代码无论如何都不会是可移植的),您可以在创建时玩个把戏, 如果您控制创建线程的代码:
Give pthread_create()
an entry function that calls gettid()
(which by the way you are likely to have to do using the syscall
macro directly because it is not always exported by libc
), stores the result somewhere, and then calls the original entry function. If you have multiple threads with the same entry function, you can pass an incremented pointer into an array in the arg
argument to pthread_create
, which will then be passed to the entry function you created to store the thread ID in. Store the pthread_t
return value of pthread_create
in the same order, and then you will be able to look up the Linux thread IDs of all threads you created given their pthread_t
value.
给出pthread_create()
一个调用的入口函数gettid()
(顺便说一下,您可能不得不syscall
直接使用宏,因为它并不总是由 导出libc
),将结果存储在某处,然后调用原始入口函数。如果您有多个线程具有相同的入口函数,您可以将一个递增的指针传递到arg
参数中的数组to 中pthread_create
,然后将其传递给您创建的入口函数以存储线程 ID。将 的pthread_t
返回值存储pthread_create
在相同的顺序,然后您将能够查找您创建的所有线程的 Linux 线程 ID,给定它们的pthread_t
值。
Whether this trick is worth it, depends on how important setting the CPU affinity is in your case, versus not accessing internal structures of the thread library or depending on a thread library that provides pthread_setaffinity_np
.
这个技巧是否值得,取决于在您的情况下设置 CPU 关联性的重要性,而不是不访问线程库的内部结构或取决于提供pthread_setaffinity_np
.
回答by Kamil Zadora
I would suggest a simple workaround with a shared int array where you could write the thread id from the threads to access it later.
我建议使用共享 int 数组的简单解决方法,您可以在其中写入线程中的线程 id 以便稍后访问它。
Hope that helps.
希望有帮助。
回答by Eric Wang
pthread_t pthread_self()
this return current pthread_t, which is thread id, you can convert it to type "unsigned int",
此返回当前 pthread_t,即线程 ID,您可以将其转换为“unsigned int”类型,
回答by jayadev
Actually pthread_self
returns pthread_t
and not a integer thread id you can work with, the following helper function will get you that in a portable way across different POSIX systems.
实际上pthread_self
返回pthread_t
而不是您可以使用的整数线程 id,以下帮助函数将以可移植的方式在不同的 POSIX 系统中为您提供。
uint64_t gettid() {
pthread_t ptid = pthread_self();
uint64_t threadId = 0;
memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
return threadId;
}
回答by Giuseppe Corbelli
In glibc 2.24the pthread_t
returned is just the pointer to an opaque struct pthread
. You can look up the definition in nptl/descr.h
.
在glibc 2.24 中,pthread_t
返回的只是指向 opaque 的指针struct pthread
。您可以在 中查找定义nptl/descr.h
。