-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_pool.cpp
49 lines (40 loc) · 1.17 KB
/
thread_pool.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
46
47
48
49
#include <assert.h>
#include "thread_pool.h"
static void *worker(void *arg) {
TheadPool *tp = (TheadPool *)arg;
while (true) {
pthread_mutex_lock(&tp->mu);
// wait for the condition: a non-empty queue
while (tp->queue.empty()) {
pthread_cond_wait(&tp->not_empty, &tp->mu);
}
// got the job
Work w = tp->queue.front();
tp->queue.pop_front();
pthread_mutex_unlock(&tp->mu);
// do the work
w.f(w.arg);
}
return NULL;
}
void thread_pool_init(TheadPool *tp, size_t num_threads) {
assert(num_threads > 0);
int rv = pthread_mutex_init(&tp->mu, NULL);
assert(rv == 0);
rv = pthread_cond_init(&tp->not_empty, NULL);
assert(rv == 0);
tp->threads.resize(num_threads);
for (size_t i = 0; i < num_threads; ++i) {
int rv = pthread_create(&tp->threads[i], NULL, &worker, tp);
assert(rv == 0);
}
}
void thread_pool_queue(TheadPool *tp, void (*f)(void *), void *arg) {
Work w;
w.f = f;
w.arg = arg;
pthread_mutex_lock(&tp->mu);
tp->queue.push_back(w);
pthread_cond_signal(&tp->not_empty);
pthread_mutex_unlock(&tp->mu);
}