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

在 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)。

https://stackoverflow.com/questions/21091000/

相关文章:

linux - Shell - 将变量内容写入文件

linux - 如何从文本文件中删除非 UTF-8 字符

python - Anaconda 与 miniconda

linux - 如何 RSYNC 单个文件?

unix - 如何交错来自两个文本文件的行

python - 在批处理文件中休眠

python - 如何在 Windows 上使用 Python 3 连接到 MySQL?

python - 在 IPython 中自动重新加载模块

c - 如何使用 GDB 修改内存内容?

python - 在 Python 中 Ping 服务器