-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadpool.hpp
165 lines (140 loc) · 4.53 KB
/
threadpool.hpp
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
160
161
162
163
164
165
#ifndef _THREADPOOL_HPP
#define _THREADPOOL_HPP
#include <condition_variable>
#include <exception>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
namespace tp {
enum TaskStatus {
TASK_STATUS_RUNNING,
TASK_STATUS_SUCCESS,
TASK_STATUS_FAILURE,
};
class Task {
protected:
std::mutex mutex;
std::condition_variable cv;
public:
TaskStatus status = TASK_STATUS_RUNNING;
std::function<void(void*)> func;
void* arg = nullptr;
std::exception error;
Task(std::function<void(void*)> func, void* arg = nullptr):
func(std::move(func)),
arg(arg) {}
void execute() {
mutex.lock();
try {
func(arg);
status = TASK_STATUS_SUCCESS;
} catch (const std::exception& e) {
error = e;
status = TASK_STATUS_FAILURE;
}
mutex.unlock();
cv.notify_all();
}
TaskStatus await() {
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this]() {
return status != TASK_STATUS_RUNNING;
});
return status;
}
};
class ThreadPool {
protected:
void runner() {
mutex.lock();
++thread_count;
mutex.unlock();
cv.notify_all();
std::unique_lock<std::mutex> lock(mutex);
for (;; cv.wait(lock)) {
while (!queue.empty()) {
std::shared_ptr<Task> task = std::move(queue.front());
queue.pop();
lock.unlock();
task->execute();
lock.lock();
--busy_count;
}
if (target_thread_count < thread_count) {
break;
}
}
--thread_count;
cv.notify_all();
}
std::mutex mutex;
std::condition_variable cv;
std::queue<std::shared_ptr<Task>> queue;
unsigned int thread_count;
unsigned int target_thread_count;
unsigned int busy_count;
public:
ThreadPool(unsigned int size = std::thread::hardware_concurrency()):
thread_count(0),
target_thread_count(size),
busy_count(0) {
for (unsigned int i = 0; i < target_thread_count; ++i) {
std::thread(&ThreadPool::runner, this).detach();
}
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this]() {
return thread_count == target_thread_count;
});
}
~ThreadPool() {
std::unique_lock<std::mutex> lock(mutex);
target_thread_count = 0;
lock.unlock();
cv.notify_all();
lock.lock();
cv.wait(lock, [this]() {
return !thread_count;
});
}
std::shared_ptr<Task> schedule(std::function<void(void*)> func, void* arg = nullptr, bool launch_if_busy = false) {
std::shared_ptr<Task> task = std::make_shared<Task>(std::move(func), arg);
if (launch_if_busy) {
std::unique_lock<std::mutex> lock(mutex);
if (busy_count >= thread_count) {
lock.unlock();
std::thread(&Task::execute, task).detach();
return task;
}
}
mutex.lock();
queue.push(task);
++busy_count;
mutex.unlock();
cv.notify_one();
return task;
};
void resize(unsigned int size) {
std::unique_lock<std::mutex> lock(mutex);
if (size < target_thread_count) {
target_thread_count = size;
cv.notify_all();
} else if (size > target_thread_count) {
for (unsigned int i = 0; i < size - target_thread_count; ++i) {
std::thread(&ThreadPool::runner, this).detach();
}
target_thread_count = size;
}
cv.wait(lock, [this]() {
return thread_count == target_thread_count;
});
}
// This function cannot be const due to its locking of the mutex
unsigned int size() {
std::lock_guard<std::mutex> lock(mutex);
return target_thread_count;
}
};
} // namespace tp
#endif