0
#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运行这个程序。pthread_self()不返回有意义的线程ID?
$gcc testtid.c -lpthread && ./a.out
pid=35086,tid=35086,pt_tid=1295541984
似乎能系统调用给出正确的线程ID(同样喜欢进程ID),但pthread_self没有给出有意义的结果。
是否因为pthread_self不可移植?
该值唯一标识线程。它不打算以任何其他方式有意义。特别是它不一定等于任何内核损坏的标识符。 –
所以,你的意思是它并不表示任何“linux”上下文值,仅用于POSIX可移植性,对吧? –
是的,就是这个。 –