开发者社区> 问答> 正文

pthread_join() 会改变线程id吗?

今天看Linux多线程,用pthread_create()创建了一个线程,然后在主线程里用pthread_join()等待其退出,但是发现一个奇怪的现象,在pthread_join()函数传递的第二个参数非空的时候,主线程里保存的创建的线程的id被改变了。第二个参数为NULL时就不会。

代码如下:

#include <stdio.h>
#include <pthread.h>

void *thread_join_test(void *args)
{
    printf("in thread_join_test thread id: %lu\n", pthread_self());
}

int main(void)
{
    pthread_t thread_id;
    int statu;

    pthread_create(&thread_id, NULL, (void *)thread_join_test, NULL);
    printf("in main before join thread id: %lu\n", thread_id);
    pthread_join(thread_id, (void *)&statu);
//    pthread_join(thread_id, NULL);
    printf("in main after join thread id: %lu\n", thread_id);

    sleep(3);

    return 0;
}
pthread_join()参数第二个为NULL输出:

in main before join thread id: 140221570873088
in thread_join_test thread id: 140221570873088
in main after join thread id: 140221570873088
pthread_join()参数第二个为(void *)&statu输出:

in main before join thread id: 140593447913216
in thread_join_test thread id: 140593447913216
in main after join thread id: 140591459467264

为什么会不一样? 这里的thread_id是pthread_t类型变量也就是个unsigned long int类型的局部变量,做形参传到pthread_join()里怎么会变?

展开
收起
a123456678 2016-06-12 09:57:44 2039 0
1 条回答
写回答
取消 提交回答
  • 你这个是堆栈破坏了,你声明的int是32位的,而你当做void 传入了,void 是64位的指针,因此,join的时候写了64位数据,把thread_id变量给破坏了,所以你会看到有32位数据不对。你用%lx打印thread_id就清楚了。
    你这个应该有编译告警,然后加了强制类型转换,原类型应该是void **,搞清楚为啥编译告警前不要轻易强制类型转换。

    2019-07-17 19:33:15
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载