forked from zappala/pthreads-examples-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cc
83 lines (65 loc) · 1.75 KB
/
example.cc
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// C includes
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <vector>
using namespace std;
// prototype for thread routine
void* print(void*);
// structure to hold data passed to a thread
typedef struct thdata_ {
int number;
} thdata;
int
main(int argc, char **argv) {
int option;
// setup default arguments
int num_threads = 10;
// process command line options using getopt()
// see "man 3 getopt"
while ((option = getopt(argc,argv,"n:")) != -1) {
switch (option) {
case 'n':
num_threads = atoi(optarg);
break;
default:
cout << "server [-p port]" << endl;
exit(EXIT_FAILURE);
}
}
// initialize random seed:
srand(time(NULL));
// keep track of vectors
std::vector<pthread_t*> threads;
for (int i=0; i<num_threads; i++) {
// setup thread data
// notice how each thread needs its own memory!
pthread_t* thread = new pthread_t;
thdata* data = new thdata;
data->number = i+1;
// create thread
pthread_create(thread, NULL, &print, (void *) data);
threads.push_back(thread);
}
// wait for threads to terminate.
for (int i=0; i<num_threads; i++) {
pthread_join(*threads[i], NULL);
delete threads[i];
}
}
void* print(void* ptr) {
thdata* data;
data = (thdata*) ptr;
// generate random number
float r = (float)rand()/((float)RAND_MAX/5);
// sleep
sleep(r);
cout << "Thread " << data->number << " says hello." << endl;
delete data;
pthread_exit(0);
}