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

阅读 4.8k
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);

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题