-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.h
184 lines (141 loc) · 4.33 KB
/
threadpool.h
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
#include <atomic>
#include <iostream>
class ThreadPool {
public:
ThreadPool(size_t minThreads, size_t maxThreads);
~ThreadPool();
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type>;
void setMaxTasks(size_t maxTasks);
size_t getMaxTasks() const;
void setMaxThreads(size_t maxThreads);
size_t getMaxThreads() const;
size_t getTaskCount() const;
size_t getThreadCount() const;
void waitAll();
void stop();
private:
void workerThread();
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queueMutex;
std::condition_variable condition;
std::atomic<bool> stop_;
std::atomic<size_t> maxTasks;
std::atomic<size_t> maxThreads;
std::atomic<size_t> activeThreads;
std::atomic<size_t> taskCount;
std::atomic<size_t> finishedTasks;
std::condition_variable allTasksFinished;
std::mutex finishedMutex;
};
inline ThreadPool::ThreadPool(size_t minThreads, size_t maxThreads)
: stop_(false), maxTasks(100), maxThreads(maxThreads), activeThreads(0), taskCount(0), finishedTasks(0) {
if (minThreads > maxThreads) {
throw std::invalid_argument("minThreads cannot be greater than maxThreads");
}
size_t numThreads = std::thread::hardware_concurrency();
if (numThreads == 0) {
numThreads = 1;
}
if (minThreads > numThreads) {
numThreads = minThreads;
}
if (maxThreads < numThreads) {
maxThreads = numThreads;
}
workers.reserve(numThreads);
for (size_t i = 0; i < numThreads; ++i) {
workers.emplace_back(&ThreadPool::workerThread, this);
}
}
inline ThreadPool::~ThreadPool() {
stop();
}
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> {
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queueMutex);
if (stop_) {
throw std::runtime_error("enqueue on stopped ThreadPool");
}
condition.wait(lock, [this] { return tasks.size() < maxTasks.load(); });
tasks.emplace([task]() { (*task)(); });
taskCount++;
}
condition.notify_one();
return res;
}
inline void ThreadPool::workerThread() {
activeThreads++;
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queueMutex);
condition.wait(lock, [this] { return stop_ || !tasks.empty(); });
if (stop_ && tasks.empty()) {
activeThreads--;
return;
}
task = std::move(tasks.front());
tasks.pop();
}
task();
{
std::unique_lock<std::mutex> lock(finishedMutex);
finishedTasks++;
if (finishedTasks == taskCount) {
allTasksFinished.notify_all();
}
}
}
}
inline void ThreadPool::setMaxTasks(size_t maxTasks) {
this->maxTasks.store(maxTasks);
}
inline size_t ThreadPool::getMaxTasks() const {
return maxTasks.load();
}
inline void ThreadPool::setMaxThreads(size_t maxThreads) {
this->maxThreads.store(maxThreads);
}
inline size_t ThreadPool::getMaxThreads() const {
return maxThreads.load();
}
inline size_t ThreadPool::getTaskCount() const {
return taskCount.load();
}
inline size_t ThreadPool::getThreadCount() const {
return activeThreads.load();
}
inline void ThreadPool::waitAll() {
std::unique_lock<std::mutex> lock(finishedMutex);
allTasksFinished.wait(lock, [this] { return finishedTasks == taskCount.load(); });
}
inline void ThreadPool::stop() {
{
std::unique_lock<std::mutex> lock(queueMutex);
stop_ = true;
}
condition.notify_all();
for (std::thread& worker : workers) {
worker.join();
}
}
#endif