免费注册 查看新帖 |

Chinaunix

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

pthread_self和gettid [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-16 13:46 |只看该作者 |倒序浏览
请问这两个函数或系统调用是不是一回事?我这里它们的返回值是不等的。
比如下面这个程序:

  1. #define _GNU_SOURCE

  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <pthread.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <sys/syscall.h>


  9. void *thsf(void *arg){
  10.         pthread_t th;
  11.         char *retstr;
  12.         retstr=malloc(50);
  13.         memset(retstr,0,50);
  14.         strcpy(retstr,"New thread quit!");
  15.         th=pthread_self();
  16.         printf("New thread,pid:%d,tid:%x,another tid:%x.\n",getpid(),th,(long)syscall(SYS_gettid));
  17.         pthread_exit(retstr);
  18. }


  19. int main(){
  20.         pthread_t nth1,mth,oth;
  21.         void *a;
  22.         if(pthread_create(&nth1,NULL,thsf,NULL))
  23.                 printf("Error create new thread.\n");
  24.         mth=pthread_self();
  25.         printf("Main thread,pid:%d,tid:%x,another tid:%x.\n",getpid(),mth,(long)syscall(SYS_gettid));
  26.         if(!pthread_join(nth1,&a)){
  27.                 printf("New thread quit,message from new thread:%s.\n",(char *)a);
  28.                 free((char *)a);
  29.         }
  30.         return 0;

  31. }
复制代码

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
2 [报告]
发表于 2007-07-16 13:49 |只看该作者
两种东西

论坛徽章:
0
3 [报告]
发表于 2007-07-16 13:52 |只看该作者
糊涂了,在主线程中gettid跟getpid返回值一致,在新建线程中gettid=getpid+1。

如果使用linuxthreads线程库,它们应该是一致的吧?

[ 本帖最后由 hohoxu_hao115 于 2007-7-16 13:57 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP