-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2.cpp
45 lines (36 loc) · 917 Bytes
/
demo2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
struct thread_data{
int thread_id;
char *message;
};
void *PrintHello(void *threadarg)
{
struct thread_data *my_data;
my_data=(struct thread_data *)threadarg;
cout<<"id:"<<my_data->thread_id<<endl;
cout<<"mess:"<<my_data->message<<endl;
pthread_exit(NULL);
return 0;
}
int main ()
{
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc;
int i;
for(i=0;i<NUM_THREADS;i++){
cout<<"main():creating thread,"<<i<<endl;
td[i].thread_id=i;
td[i].message=(char*)"this is a message";
rc=pthread_create(&threads[i],NULL,PrintHello,(void*)&td[i]);
if(rc){
cout<<"error"<<rc<<endl;
exit(-1);
}
}
pthread_exit(NULL);
}