linux - pthread_join() 会改变线程id吗?
天蓬老师
天蓬老师 2017-04-17 13:50:43
[Linux讨论组]

今天看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()里怎么会变?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
高洛峰

你这个是堆栈破坏了,你声明的int是32位的,而你当做void *传入了,void *是64位的指针,因此,join的时候写了64位数据,把thread_id变量给破坏了,所以你会看到有32位数据不对。你用%lx打印thread_id就清楚了。
你这个应该有编译告警,然后加了强制类型转换,原类型应该是void **,搞清楚为啥编译告警前不要轻易强制类型转换。

伊谢尔伦

thread id不是通过pthread_t获得的, pthread_t是一个句柄一样的东西, 得通过系统调用获得线程ID
syscall(SYS_gettid);

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号