gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:26:27 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com