c - 如何在linux c程序中获取pthread的线程ID?

标签 c linux pthreads

在 Linux C 程序中,如何打印由 pthread 库创建的线程的线程 ID?例如我们如何通过 getpid() 获取进程的 pid。

最佳答案

什么?该人要求特定于 Linux,以及 getpid() 的等价物。不是 BSD 或苹果。答案是 gettid() 并返回一个整数类型。您必须使用 syscall() 调用它,如下所示:

#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

 ....

 pid_t x = syscall(__NR_gettid);

虽然这可能无法移植到非 Linux 系统,但 threadid 可以直接比较并且获取速度非常快。它可以像普通整数一样打印(例如用于 LOG)。

关于c - 如何在linux c程序中获取pthread的线程ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21091000/

相关文章:

linux - 转义 linux shell 参数

c++ - 为什么 cout 不适用于 pthreads?

c - 将多个文件内容写入单个文件的有效方法

c - 是否有 gcc 命令行选项来消除警告 : passing argument n discards qualifiers from type

c - 如何更改 termios 配置,以便在用户按下 <tab> 键时 getc() 立即返回?

通过引用返回的 C 风格字符串

c - 如何在C中使用pthread来统计单词出现的次数?

c++ - 如何创建 4096 对齐表[x][4096]

linux - Linux 上/etc/hosts 中的 Curl 脚本

linux - 将 shell 脚本和 zip 文件组合成一个可执行文件以进行部署