-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pool.cpp
159 lines (109 loc) · 3.33 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "thread_pool.h"
thread_pool::task_queue::task_queue() : head(nullptr), tail(nullptr), n_tasks(0) {
// TODO: Error checking
pthread_cond_init(&queue_available, NULL);
pthread_mutex_init(&queue_rwlock, NULL);
pthread_cond_init(&queue_empty, NULL);
};
thread_pool::task* thread_pool::task_queue::next() {
if (n_tasks == 0)
return nullptr;
task* ret = head->task;
task_queue_node* next = head->next;
delete head;
n_tasks--;
head = next;
return ret;
}
void thread_pool::task_queue::insert(task* task) {
task_queue_node *new_node = new task_queue_node(task);
// TODO: Error checking
pthread_mutex_lock(&queue_rwlock);
// Insert into queue
if (n_tasks == 0) {
head = new_node;
tail = new_node;
} else {
tail->next = new_node;
tail = new_node;
}
n_tasks++;
// Signal any thread waiting to read from the queue.
pthread_cond_signal(&queue_available);
// Unlock the lock.
pthread_mutex_unlock(&queue_rwlock);
}
void thread_pool::task_queue::lock() {
pthread_mutex_lock(&queue_rwlock);
}
void thread_pool::task_queue::unlock() {
pthread_mutex_unlock(&queue_rwlock);
}
void thread_pool::task_queue::wait() {
pthread_cond_wait(&queue_available, &queue_rwlock);
}
void thread_pool::task_queue::broadcast() {
pthread_cond_broadcast(&queue_available);
}
thread_pool::task_queue::~task_queue(){
pthread_mutex_destroy(&queue_rwlock);
pthread_cond_destroy(&queue_available);
pthread_cond_destroy(&queue_empty);
while (head != nullptr) {
task_queue_node* next = head->next;
delete head;
head = next;
}
}
thread_pool::thread_pool(int n_threads) : running(1), n_threads(n_threads), active(0) {
threads = new pthread_t[n_threads];
// TODO: Error checking
// Initialize threads
for (int i = 0; i < n_threads; ++i)
pthread_create(threads + i, NULL, thread_run, this);
}
void thread_pool::add_task(task* task) {
task_queue.insert(task);
}
void* thread_pool::thread_run(void *t_pool) {
thread_pool* pool = (thread_pool*) t_pool;
for (;;) {
// Acquire lock since the get function requires it
pool->task_queue.lock();
while (pool->task_queue.n_tasks == 0 && pool->running == 1)
pool->task_queue.wait();
if (pool->running == 0 && pool->task_queue.n_tasks == 0)
break;
task* task = pool->task_queue.next();
pool->active++;
pool->task_queue.unlock();
task->run();
delete task;
pool->task_queue.lock();
pool->active--;
if (pool->active == 0 && pool->task_queue.n_tasks == 0)
pool->task_queue.broadcast();
pool->task_queue.unlock();
}
pool->task_queue.unlock();
pthread_exit(NULL);
return NULL;
}
void thread_pool::task_queue::broadcast_queue_empty() {
pthread_cond_broadcast(&queue_empty);
}
void thread_pool::wait_all() {
task_queue.lock();
while (task_queue.n_tasks != 0 || active != 0)
task_queue.wait();
task_queue.unlock();
}
thread_pool::~thread_pool() {
task_queue.lock();
running = 0;
task_queue.broadcast();
task_queue.unlock();
for (int i = 0; i < n_threads; ++i)
pthread_join(threads[i], NULL);
delete[] threads;
}