线程函数pthread_join

本文深入探讨了pthread_join函数的使用方法,该函数用于在多线程环境中回收子线程并获取其退出状态。通过具体示例代码,展示了如何在主线程中等待子线程结束,并从子线程中获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_join函数用于回收子线程,并且可以获取到子线程的退出状态;

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <unistd.h>
  4 #include <pthread.h>
  5 
  6 
  7 
  8 typedef struct
  9 {
 10         int a;
 11         int b;
 12 }exit_t;
 13 
 14 
 15 void* tnf(void* arg)
 16 {
 17         exit_t* ret = (exit_t*)arg;
 18         //ret = malloc(sizeof(exit_t));
 19         ret->a = 100;
 20         ret->b = 300;
 21         pthread_exit((void*)ret);
 22 }
 23 
 24 int main()
 25 {
 26 pthread_t tid;
 27 //exit_t** ret;
 28 exit_t* ret =(exit_t*) malloc(sizeof(exit_t));
 29 pthread_create(&tid,NULL,tnf,(void*)ret);
 30 pthread_join(tid,(void**)&ret);
 31 printf("a= %d,b= %d\n",ret->a,ret->b);
 32 free(ret);
 33 return 0;
 34 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值