免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3870 | 回复: 5
打印 上一主题 下一主题

无法打印出线程号,这是为什么呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-29 15:34 |只看该作者 |倒序浏览
程序a.c如下
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>

  4. void *myThread(void *arg)
  5. {
  6.    sleep(1000);
  7.    return 0;
  8. }
  9. main()
  10. {
  11.    int ret;
  12.    char tmpstr[20];

  13.    pthread_t mythread=0;
  14.    ret = pthread_create( &mythread, NULL, myThread, NULL );
  15.    printf("tid=%lu\n",mythread);
  16.    ret = pthread_create( &mythread, NULL, myThread, NULL );
  17.    printf("tid=%lu\n",mythread);
  18.    ret = pthread_create( &mythread, NULL, myThread, NULL );
  19.    printf("tid=%lu\n",mythread);
  20.    ret = pthread_create( &mythread, NULL, myThread, NULL );
  21.    printf("tid=%lu\n",mythread);

  22.    printf("tid=%lu\n",mythread);



  23.    sleep(1000);
  24. }
复制代码
编译:
gcc  a.c -lpthread生成a.out
执行a.out输出
tid=3086244752
tid=3075750800
tid=3065260944
tid=3054771088
tid=3044281232
而通过执行pstree 命令
$ pstree -p 17384
其中17384是进程号
a.out(17384)-+-{a.out}(17385)
            |-{a.out}(17386)
            |-{a.out}(17387)
            |-{a.out}(1738
            `-{a.out}(17389)
为什么打印的不对呢?
用printf("%u",tid)也相同
用printf("%d",tid)竟然是负数,当然也不对。
该如何输出呢?

论坛徽章:
0
2 [报告]
发表于 2010-10-29 16:49 |只看该作者
pthread_self()

论坛徽章:
0
3 [报告]
发表于 2010-10-29 16:54 |只看该作者
getppid

论坛徽章:
1
CU十二周年纪念徽章
日期:2013-10-24 15:41:34
4 [报告]
发表于 2010-10-29 16:55 |只看该作者
LZ这样输出应该是对的,在linux中pthread_t应该是个无符号整数。

论坛徽章:
0
5 [报告]
发表于 2010-10-29 17:13 |只看该作者
回复 4# ecjtubaowp


    知音!可打印咋就不对呢,在AIX 6就是对!

论坛徽章:
1
CU十二周年纪念徽章
日期:2013-10-24 15:41:34
6 [报告]
发表于 2010-10-29 17:31 |只看该作者
回复 5# demaple


    你用pstree打印的我就不清楚了,你看看/proc/pid/task,这个目录是该进程的所有线程,包含pid主线程
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP