c++多线程参数的传递

#include <iostream>
#include <pthread.h>        //多线程相关操作头文件,可移植众多平台
using namespace std;

struct mypara
{      
    int para1;            //参数1       
    char *para2;        //参数2
    pthread_t wait;
};

void* thread1( void* args )  //本函数演示的是数据的传出
{
    mypara *my = (mypara *)args;
    srand(unsigned(time(0)));
    my->para1 = rand()%100;
    my->para2 = "";
    cout << "给结构体赋值结束"<< endl;
    return 0;
} 
void* thread2( void* args ) //本函数演示的是数据的传入
{
    mypara *str_in = (mypara *)args;
    cout << "线程2开始运行........................" <<endl;
    pthread_join(str_in->wait,NULL);    //需要等待thread1线程给struct 结构体赋值结束后才能运行
    cout << "线程1产生的随机数是:" << str_in->para1<< " "<< str_in->para2 <<endl;
    return 0;
} 
int main()
{
    pthread_t tid1,tid2; 
    mypara my_para;

    int ret = pthread_create( &tid1, NULL, thread1, (void *)&my_para );    
    //pthread_join(tid1,NULL);
    my_para.wait = tid1;
    pthread_create( &tid2, NULL, thread2, (void *)&my_para );
    system("pause");
    pthread_exit( NULL ); 
}

c++多线程参数的传递——通过结构体传递参数。(pthread多线程类库,不能在x64位上编译,【C++ 11 自带多线程】)

转载于:https://www.cnblogs.com/lwngreat/p/4280226.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值