forked from SeanBarber/homework7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro_threadpool.c
98 lines (90 loc) · 2.59 KB
/
macro_threadpool.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUMTASKS 25000
#define NANOSECONDS_PER_SECOND 1E9
#define FIBNUM 26
int numworkers = 2;
sem_t available;
pthread_mutex_t protect_state;
struct worker {
pthread_t tid;
long input; // if parent sees input = -1, worker available. if worker sees input = -1, work is done
long output; // if output = -1, nothing done yet
sem_t done;
};
struct worker * workers;
int fib(int x) {
if (x == 0)
return 0;
else if (x == 1)
return 1;
return fib(x - 1) + fib(x - 2);
}
void * workerthread(void * tid){
int i = (long)tid;
while(1){
sem_wait(&workers[i].done);
if(workers[i].input == -1)
return NULL;
long result = fib(workers[i].input);
pthread_mutex_lock(&protect_state);
workers[i].output = result;
workers[i].input = -1;
pthread_mutex_unlock(&protect_state);
sem_post(&available);
}
}
int main(int argc, char ** argv){
// If you want to use a different number of workers, specify it as the first
// argument on the command line
if(argc == 2){
numworkers = atoi(argv[1]);
}
workers = malloc(numworkers * sizeof(struct worker));
sem_init(&available,0,numworkers);
pthread_mutex_init(&protect_state,NULL);
for(long i = 0; i < numworkers; i++){
sem_init(&workers[i].done,0,0);
workers[i].output = -1;
workers[i].input = -1;
if(pthread_create(&workers[i].tid,NULL,workerthread,(void*)i) != 0){
perror("pthread_create failed: ");
exit(1);
}
}
printf("Running fib(%d) %d times with %d worker threads\n",FIBNUM,NUMTASKS,numworkers);
for (int j=0; j<NUMTASKS; j++){
sem_wait(&available);
pthread_mutex_lock(&protect_state);
for(int i = 0; i < numworkers; i++){
if (workers[i].input == -1){
if (workers[i].output != -1){
// If you cared about output, you would do something with it here, e.g.:
// printf("worker %d finished task with result %ld\n",i,workers[i].output);
}
workers[i].input = FIBNUM;
sem_post(&workers[i].done);
break;
}
}
pthread_mutex_unlock(&protect_state);
};
// drain the final results
for (int j = 0; j < numworkers; j++){
sem_wait(&available);
pthread_mutex_lock(&protect_state);
for(int i = 0; i < numworkers; i++){
if (workers[i].input == -1){
if (workers[i].output != -1){
// If you cared about output, you would do something with it here
}
workers[i].input = -1;
sem_post(&workers[i].done);
}
}
pthread_mutex_unlock(&protect_state);
}
return 0;
}