Linux 如何从pthread获取pid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7271154/
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 to get pid from pthread
提问by Shawn
in RH Linux, every pthread is mapping to a pid, which can be monitored in tools such as htop. but how can i get the pid of a thread? getpid() just return the pid of the main thread.
在 RH Linux 中,每个 pthread 都映射到一个 pid,可以在 htop 等工具中对其进行监控。但是我怎样才能得到一个线程的pid呢?getpid() 只返回主线程的 pid。
采纳答案by Alok Save
Can be called to return the ID of the calling thread.
可以调用返回调用线程的ID。
Also PID is process Id, A thread has thread Id not PID. All threads running in the same process will have the same PID.
PID 也是进程 ID,线程的线程 ID 不是 PID。在同一进程中运行的所有线程将具有相同的 PID。
回答by R. Martinho Fernandes
A PID is a Process ID, not a thread ID. Threads running on the same process will obviously all be associated with the same PID.
PID 是进程 ID,而不是线程 ID。运行在同一个进程上的线程显然都与同一个 PID 相关联。
Because pthreads tries to be portable you cannot obtain the ID of the underlying OS thread directly. It's even possible that there isn't an underlying OS thread.
因为 pthreads 试图是可移植的,所以您无法直接获取底层操作系统线程的 ID。甚至可能没有底层操作系统线程。
回答by Aif
I think the function you are looking for is pthread_self
我认为您正在寻找的功能是pthread_self
回答by John Humphreys - w00te
Threads have tids (threadIds), and all threads run in the same process (pid). So, your threads should all have the same pid assuming they're created in the same process, bu they'll have different tids.
线程有tids(threadIds),所有线程都运行在同一个进程(pid)中。因此,假设您的线程是在同一进程中创建的,则它们都应该具有相同的 pid,但是它们将具有不同的 tid。
pthread_self() gives tid, and getpid() gets the pid.
pthread_self() 给出 tid,getpid() 获取 pid。
回答by Duck
There are two thread values that get confused. pthread_self()will return the POSIX thread id; gettid()will return the OS thread id. The latter is linux specific and not guaranteed to be portable but probably what you are really looking for.
有两个线程值会混淆。 pthread_self()将返回 POSIX 线程 ID;gettid()将返回操作系统线程 ID。后者是特定于 linux 的,不能保证是可移植的,但可能是您真正需要的。
EDITAs PlasmaHH notes, gettid()
is called via syscall()
. From the syscall()
man page:
编辑正如 PlasmaHH 所指出的,gettid()
被称为 via syscall()
。从syscall()
手册页:
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
int
main(int argc, char *argv[])
{
pid_t tid;
tid = syscall(SYS_gettid);
}
回答by alex
pthread_self does notget the tid. it proviedes a handle or pointer of type pthread_t for usage in pthread functions.
pthread_self没有得到 tid。它提供了一个 pthread_t 类型的句柄或指针,供 pthread 函数使用。
see here for an example what a real world program might return:
在这里查看真实世界程序可能返回的示例:
回答by jayadev
Actually pthread_self
return 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;
}