我看到在htop的树模式下我的多线程程序下有几个进程。我知道它们是线程 ID。但是这个 id 与 pthread_create 函数返回的线程 id 不匹配。
int _id = pthread_create(&m_iAudioThreadID, NULL, AudioRecvThread, this);
m_iAudioThreadID
是否应该等于我们在 htop 的树模式中看到的进程的 PID?但事实并非如此。如何从我的程序中以编程方式找到 htop 的 PID?谢谢。
最佳答案
Is the m_iAudioThreadID supposed to be equal to the PID we see in htop's tree mode for a process?
不,他们不是。 htop
显示进程 ID、PID。 pthread_create()
设置的 PThread-ID 不同:Distinction between processes and threads in Linux
一个主要区别是 PID 唯一标识系统现有进程中的进程,PThread-ID 唯一标识进程现有线程中的线程。
How do I find the PID of htop's programmatically from my program?
至少在最近的 Linux 上:要获取与某个 PThread 相关联的 PID,请使用相关线程内的 gettid()
系统调用:
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
关于c++ - 以编程方式获取与 htop 的相应 pid 匹配的进程的线程 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526301/