父子进程之间的参数传递和数据返回 pthread_create & pthread_join

本文介绍了如何在Linux中使用pthread_create和pthread_join进行父子线程间参数传递和数据返回。通过示例展示了主线程传递结构体地址、字符串以及子线程通过pthread_exit返回数据的方法,并探讨了参数类型匹配和指针转换的重要性。
摘要由CSDN通过智能技术生成

自己睡不着敲着玩的,第一个程序是主线程传递一个结构体地址给子线程,第二个传递字符串,第三个是子线程通过pthread_exit()传递数据,然后pthread_join阻塞那里捕获。

 int pthread_create(pthread_t *restrict thread,
                  const pthread_attr_t *restrict attr,
                  void *(*start_routine)(void*), 
   void *restrict arg);


下面说明一下各个参数的含义:
thread :所创建的线程号。
attr :所创建的线程属性,这个将在后面详细说明。
start_routine :即将运行的线程函数。
art :传递给线程函数的参数。


防止main线程提前退出,pthread_join 函数会让主线程阻塞,直到所有线程都已经退出。


int pthread_join(pthread_t thread, void **value_ptr);
thread :等待退出线程的线程号。
value_ptr :退出线程的返回值。


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#inc
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值