LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 979|回复: 2

问一个linux下线程ID的问题

[复制链接]
发表于 2005-9-12 16:35:11 | 显示全部楼层 |阅读模式
我在linux环境下想得到线程ID号,使用如下语句:
  printf("The threadID is %d\n", pthread_self());

但结果却是  
The threadID is -1208034384

不是我想象的0,请教这是怎么回事?

我用的是redhat 企业版,PC机。
发表于 2005-9-12 17:02:18 | 显示全部楼层
man gettid
回复 支持 反对

使用道具 举报

发表于 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



  1. #include<pthread.h>
  2. #include<sys/types.h>
  3. #include<linux/unistd.h>
  4. #include<errno.h>
  5. _syscall0(pid_t, gettid);

  6. int main(){
  7.         printf("%d, %d, %u\n", getpid(), gettid(), pthread_self());
  8. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表