-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursive_add_work.c
76 lines (65 loc) · 1.94 KB
/
recursive_add_work.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
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "thread_pool.h"
const size_t NUM_THREADS = 16;
const size_t MAX_DEPTH = 5;
const size_t BRANCH_FACTOR = 5;
thread_pool_t *pool;
size_t remaining_work;
pthread_mutex_t remaining_work_mutex;
pthread_cond_t done_cond;
void recursive_work(void *);
void add_work(size_t depth) {
size_t *aux = malloc(sizeof(size_t));
assert(aux != NULL);
*aux = depth;
thread_pool_add_work(pool, recursive_work, aux);
int result = pthread_mutex_lock(&remaining_work_mutex);
assert(result == 0);
remaining_work++;
result = pthread_mutex_unlock(&remaining_work_mutex);
assert(result == 0);
}
void recursive_work(void *_depth) {
size_t depth = *(size_t *) _depth;
free(_depth);
if (depth == MAX_DEPTH) {
printf("Recursion depth hit!\n");
}
else {
for (size_t i = 0; i < BRANCH_FACTOR; i++) {
add_work(depth + 1);
}
}
int result = pthread_mutex_lock(&remaining_work_mutex);
assert(result == 0);
if (--remaining_work == 0) {
pthread_cond_signal(&done_cond);
}
result = pthread_mutex_unlock(&remaining_work_mutex);
assert(result == 0);
}
int main() {
pool = thread_pool_init(NUM_THREADS);
remaining_work = 0;
int result = pthread_mutex_init(&remaining_work_mutex, NULL);
assert(result == 0);
result = pthread_cond_init(&done_cond, NULL);
assert(result == 0);
add_work(0);
result = pthread_mutex_lock(&remaining_work_mutex);
assert(result == 0);
while (remaining_work > 0) {
result = pthread_cond_wait(&done_cond, &remaining_work_mutex);
assert(result == 0);
}
result = pthread_mutex_unlock(&remaining_work_mutex);
assert(result == 0);
thread_pool_finish(pool);
result = pthread_mutex_destroy(&remaining_work_mutex);
assert(result == 0);
result = pthread_cond_destroy(&done_cond);
assert(result == 0);
}