c++多线程 传结构体

// 向线程传递参数(2)

#include<iostream>

#include<cstdlib>

#include<pthread.h>

 

using namespace std;

#define Numthread 5

// struct thread data

struct thread_data{

int num;

char* stringm;

}td;

// define a function

void* AlsoHW(void* threaddata)

{

struct thread_data* mydata;

mydata = (struct thread_data *)threaddata;

cout<<"This is mydata->num---->"<<mydata->num<<endl;

cout<<"This is mydata->stringm---->"<<mydata->stringm<<endl;

pthread_exit(NULL);

}

int main()

{

pthread_t thread_id[Numthread];

int index[Numthread];

struct thread_data thread_indata[Numthread];

int rc;

for (int i = 0; i < Numthread; i++)

{

cout<<"main() ---"<<i<<endl;

thread_indata[i].num = i;

thread_indata[i].stringm = "Are you OK?";

rc = pthread_create(&thread_id[i], NULL, AlsoHW, (void*)&thread_indata[i]);

}

if (rc)

{

cout<<"create thread_id"<<rc<<"failed"<<endl;

exit(-1);

}

pthread_exit(NULL);

return 0;

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值