gpt4 book ai didi

c - pthread_self() 没有返回有意义的线程 ID?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:46 28 4
gpt4 key购买 nike

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
int main(){
int systid=syscall(186);
int pt_tid=pthread_self();
pid_t id=getpid();
printf("pid=%d,tid=%d,pt_tid=%d\n",id,systid,pt_tid);
return 0;
}

我在 RHEL 5 上用 gcc4.1.2 运行这个程序。

$gcc testtid.c -lpthread && ./a.out
pid=35086,tid=35086,pt_tid=1295541984

似乎系统调用可以给出正确的线程 ID(与进程 ID 相同),但 pthread_self 没有给出有意义的结果。

是不是因为pthread_self不可移植?

最佳答案

如果您阅读 man pthread_self ,你应该:

Thread identifiers should be considered opaque: any attempt to use a thread ID other than in pthreads calls is non-portable and can lead to unspecified results.

Thread IDs are guaranteed to be unique only within a process. A thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated.

The thread ID returned by pthread_self() is not the same thing as the kernel thread ID returned by a call to gettid(2).

关于c - pthread_self() 没有返回有意义的线程 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44776152/

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