编程语言
首页 > 编程语言> > c++多线程 传结构体

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;

}

标签:struct,thread,int,indata,c++,Numthread,多线程,data,结构
来源: https://blog.csdn.net/weixin_40642281/article/details/100997477