| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef __ctpl_stl_thread_pool_H__ |
| 21 |
#define __ctpl_stl_thread_pool_H__ |
| 22 |
|
| 23 |
#include <functional> |
| 24 |
#include <thread> |
| 25 |
#include <atomic> |
| 26 |
#include <vector> |
| 27 |
#include <memory> |
| 28 |
#include <exception> |
| 29 |
#include <future> |
| 30 |
#include <mutex> |
| 31 |
#include <queue> |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
namespace ctpl { |
| 42 |
|
| 43 |
namespace detail { |
| 44 |
template <typename T> |
| 45 |
class Queue { |
| 46 |
public: |
| 47 |
bool push(T const & value) { |
| 48 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 49 |
this->q.push(value); |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
bool pop(T & v) { |
| 54 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 55 |
if (this->q.empty()) |
| 56 |
return false; |
| 57 |
v = this->q.front(); |
| 58 |
this->q.pop(); |
| 59 |
return true; |
| 60 |
} |
| 61 |
bool empty() { |
| 62 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 63 |
return this->q.empty(); |
| 64 |
} |
| 65 |
private: |
| 66 |
std::queue<T> q; |
| 67 |
std::mutex mutex; |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
class thread_pool { |
| 72 |
|
| 73 |
public: |
| 74 |
|
| 75 |
thread_pool() { this->init(); } |
| 76 |
thread_pool(int nThreads) { this->init(); this->resize(nThreads); } |
| 77 |
|
| 78 |
|
| 79 |
~thread_pool() { |
| 80 |
this->stop(true); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
int size() { return static_cast<int>(this->threads.size()); } |
| 85 |
|
| 86 |
|
| 87 |
int n_idle() { return this->nWaiting; } |
| 88 |
std::thread & get_thread(int i) { return *this->threads[i]; } |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void resize(int nThreads) { |
| 94 |
if (!this->isStop && !this->isDone) { |
| 95 |
int oldNThreads = static_cast<int>(this->threads.size()); |
| 96 |
if (oldNThreads <= nThreads) { |
| 97 |
this->threads.resize(nThreads); |
| 98 |
this->flags.resize(nThreads); |
| 99 |
|
| 100 |
for (int i = oldNThreads; i < nThreads; ++i) { |
| 101 |
this->flags[i] = std::make_shared<std::atomic<bool>>(false); |
| 102 |
this->set_thread(i); |
| 103 |
} |
| 104 |
} |
| 105 |
else { |
| 106 |
for (int i = oldNThreads - 1; i >= nThreads; --i) { |
| 107 |
*this->flags[i] = true; |
| 108 |
this->threads[i]->detach(); |
| 109 |
} |
| 110 |
{ |
| 111 |
|
| 112 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 113 |
this->cv.notify_all(); |
| 114 |
} |
| 115 |
this->threads.resize(nThreads); |
| 116 |
this->flags.resize(nThreads); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
void clear_queue() { |
| 123 |
std::function<void(int id)> * _f; |
| 124 |
while (this->q.pop(_f)) |
| 125 |
delete _f; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
std::function<void(int)> pop() { |
| 130 |
std::function<void(int id)> * _f = nullptr; |
| 131 |
this->q.pop(_f); |
| 132 |
std::unique_ptr<std::function<void(int id)>> func(_f); |
| 133 |
std::function<void(int)> f; |
| 134 |
if (_f) |
| 135 |
f = *_f; |
| 136 |
return f; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
void stop(bool isWait = false) { |
| 143 |
if (!isWait) { |
| 144 |
if (this->isStop) |
| 145 |
return; |
| 146 |
this->isStop = true; |
| 147 |
for (int i = 0, n = this->size(); i < n; ++i) { |
| 148 |
*this->flags[i] = true; |
| 149 |
} |
| 150 |
this->clear_queue(); |
| 151 |
} |
| 152 |
else { |
| 153 |
if (this->isDone || this->isStop) |
| 154 |
return; |
| 155 |
this->isDone = true; |
| 156 |
} |
| 157 |
{ |
| 158 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 159 |
this->cv.notify_all(); |
| 160 |
} |
| 161 |
for (int i = 0; i < static_cast<int>(this->threads.size()); ++i) { |
| 162 |
if (this->threads[i]->joinable()) |
| 163 |
this->threads[i]->join(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
this->clear_queue(); |
| 168 |
this->threads.clear(); |
| 169 |
this->flags.clear(); |
| 170 |
} |
| 171 |
|
| 172 |
template<typename F, typename... Rest> |
| 173 |
auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> { |
| 174 |
auto pck = std::make_shared<std::packaged_task<decltype(f(0, rest...))(int)>>( |
| 175 |
std::bind(std::forward<F>(f), std::placeholders::_1, std::forward<Rest>(rest)...) |
| 176 |
); |
| 177 |
auto _f = new std::function<void(int id)>([pck](int id) { |
| 178 |
(*pck)(id); |
| 179 |
}); |
| 180 |
this->q.push(_f); |
| 181 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 182 |
this->cv.notify_one(); |
| 183 |
return pck->get_future(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
template<typename F> |
| 189 |
auto push(F && f) ->std::future<decltype(f(0))> { |
| 190 |
auto pck = std::make_shared<std::packaged_task<decltype(f(0))(int)>>(std::forward<F>(f)); |
| 191 |
auto _f = new std::function<void(int id)>([pck](int id) { |
| 192 |
(*pck)(id); |
| 193 |
}); |
| 194 |
this->q.push(_f); |
| 195 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 196 |
this->cv.notify_one(); |
| 197 |
return pck->get_future(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
private: |
| 202 |
|
| 203 |
|
| 204 |
thread_pool(const thread_pool &); |
| 205 |
thread_pool(thread_pool &&); |
| 206 |
thread_pool & operator=(const thread_pool &); |
| 207 |
thread_pool & operator=(thread_pool &&); |
| 208 |
|
| 209 |
void set_thread(int i) { |
| 210 |
std::shared_ptr<std::atomic<bool>> flag(this->flags[i]); |
| 211 |
auto f = [this, i, flag]() { |
| 212 |
std::atomic<bool> & _flag = *flag; |
| 213 |
std::function<void(int id)> * _f; |
| 214 |
bool isPop = this->q.pop(_f); |
| 215 |
while (true) { |
| 216 |
while (isPop) { |
| 217 |
std::unique_ptr<std::function<void(int id)>> func(_f); |
| 218 |
(*_f)(i); |
| 219 |
if (_flag) |
| 220 |
return; |
| 221 |
else |
| 222 |
isPop = this->q.pop(_f); |
| 223 |
} |
| 224 |
|
| 225 |
std::unique_lock<std::mutex> lock(this->mutex); |
| 226 |
++this->nWaiting; |
| 227 |
this->cv.wait(lock, [this, &_f, &isPop, &_flag](){ isPop = this->q.pop(_f); return isPop || this->isDone || _flag; }); |
| 228 |
--this->nWaiting; |
| 229 |
if (!isPop) |
| 230 |
return; |
| 231 |
} |
| 232 |
}; |
| 233 |
this->threads[i].reset(new std::thread(f)); |
| 234 |
} |
| 235 |
|
| 236 |
void init() { this->nWaiting = 0; this->isStop = false; this->isDone = false; } |
| 237 |
|
| 238 |
std::vector<std::unique_ptr<std::thread>> threads; |
| 239 |
std::vector<std::shared_ptr<std::atomic<bool>>> flags; |
| 240 |
detail::Queue<std::function<void(int id)> *> q; |
| 241 |
std::atomic<bool> isDone; |
| 242 |
std::atomic<bool> isStop; |
| 243 |
std::atomic<int> nWaiting; |
| 244 |
|
| 245 |
std::mutex mutex; |
| 246 |
std::condition_variable cv; |
| 247 |
}; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
#endif |