|
|
发表于 2005-9-12 17:28:36
|
显示全部楼层
Post by Hamming
我在linux环境下想得到线程ID号,使用如下语句:
printf("The threadID is %d\n", pthread_self());
但结果却是
The threadID is -1208034384
不是我想象的0,请教这是怎么回事?
我用的是redhat 企业版,PC机。
不可能是0的
pthread_t 实际上是unsigned long
所以应该用printf("%u")
pthread_t只是一个handle,目的是为了区分不同的线程,深究其值具体是多少,没有意义
to weiwei:
而且在nptl里一个pthread线程的线程id(数据类型是pthread_t)和它的kernel thread id(数据类型是pid_t)不一样
在2.6Linux + nptl里,gettid返回的是pthread对应的内核线程的id, 不是pthread_t
- #include<pthread.h>
- #include<sys/types.h>
- #include<linux/unistd.h>
- #include<errno.h>
- _syscall0(pid_t, gettid);
- int main(){
- printf("%d, %d, %u\n", getpid(), gettid(), pthread_self());
- }
复制代码 |
|