35 #ifndef __ctpl_stl_thread_pool_H__ 36 #define __ctpl_stl_thread_pool_H__ 60 bool push(T
const &value) {
61 std::unique_lock<std::mutex> lock(this->mutex);
67 std::unique_lock<std::mutex> lock(this->mutex);
68 if (this->q.empty()) {
76 std::unique_lock<std::mutex> lock(this->mutex);
77 return this->q.empty();
91 this->resize(nThreads);
98 int size() {
return static_cast<int>(this->threads.size()); }
109 if (!this->isStop && !this->isDone) {
110 int oldNThreads =
static_cast<int>(this->threads.size());
111 if (oldNThreads <= nThreads) {
112 this->threads.resize(nThreads);
113 this->flags.resize(nThreads);
115 for (
int i = oldNThreads; i < nThreads; ++i) {
116 this->flags[i] = std::make_shared<std::atomic<bool>>(
false);
120 for (
int i = oldNThreads - 1; i >= nThreads; --i) {
121 *this->flags[i] =
true;
122 this->threads[i]->detach();
126 std::unique_lock<std::mutex> lock(this->mutex);
127 this->cv.notify_all();
129 this->threads.resize(
131 this->flags.resize(nThreads);
140 std::function<void(int id)> *_f;
141 while (this->q.pop(_f))
delete _f;
145 std::function<void(int)>
pop() {
146 std::function<void(int id)> *_f =
nullptr;
148 std::unique_ptr<std::function<void(int id)>> func(
150 std::function<void(int)> f;
161 void stop(
bool isWait =
false) {
167 for (
int i = 0, n = this->size(); i < n; ++i) {
168 *this->flags[i] =
true;
172 if (this->isDone || this->isStop) {
178 std::unique_lock<std::mutex> lock(this->mutex);
179 this->cv.notify_all();
181 for (
int i = 0; i < static_cast<int>(this->threads.size());
183 if (this->threads[i]->joinable()) {
184 this->threads[i]->join();
191 this->threads.clear();
195 template <
typename F,
typename... Rest>
196 auto push(F &&f, Rest &&... rest) -> std::future<decltype(f(0, rest...))> {
198 std::make_shared<std::packaged_task<decltype(f(0, rest...))(int)>>(
199 std::bind(std::forward<F>(f), std::placeholders::_1,
200 std::forward<Rest>(rest)...));
201 auto _f =
new std::function<void(int id)>([pck](
int id) { (*pck)(id); });
203 std::unique_lock<std::mutex> lock(this->mutex);
204 this->cv.notify_one();
205 return pck->get_future();
212 template <
typename F>
213 auto push(F &&f) -> std::future<decltype(f(0))> {
214 auto pck = std::make_shared<std::packaged_task<decltype(f(0))(int)>>(
216 auto _f =
new std::function<void(int id)>([pck](
int id) { (*pck)(id); });
218 std::unique_lock<std::mutex> lock(this->mutex);
219 this->cv.notify_one();
220 return pck->get_future();
230 void set_thread(
int i) {
231 std::shared_ptr<std::atomic<bool>> flag(
233 auto f = [
this, i, flag ]() {
234 std::atomic<bool> &_flag = *flag;
235 std::function<void(int id)> *_f;
236 bool isPop = this->q.pop(_f);
239 std::unique_ptr<std::function<void(int id)>> func(
247 isPop = this->q.pop(_f);
250 std::unique_lock<std::mutex> lock(this->mutex);
252 this->cv.wait(lock, [
this, &_f, &isPop, &_flag]() {
253 isPop = this->q.pop(_f);
254 return isPop || this->isDone || _flag;
262 this->threads[i].reset(
268 this->isStop =
false;
269 this->isDone =
false;
272 std::vector<std::unique_ptr<std::thread>> threads;
273 std::vector<std::shared_ptr<std::atomic<bool>>> flags;
275 std::atomic<bool> isDone;
276 std::atomic<bool> isStop;
277 std::atomic<int> nWaiting;
280 std::condition_variable cv;
284 #endif // __ctpl_stl_thread_pool_H__ bool pop(T &v)
Definition: ctpl_stl.h:66
bool push(T const &value)
Definition: ctpl_stl.h:60
int n_idle()
Definition: ctpl_stl.h:101
thread_pool(int nThreads)
Definition: ctpl_stl.h:89
auto push(F &&f, Rest &&... rest) -> std::future< decltype(f(0, rest...))>
Definition: ctpl_stl.h:196
void resize(int nThreads)
Definition: ctpl_stl.h:108
void clear_queue()
Definition: ctpl_stl.h:139
Definition: ctpl_stl.h:58
bool empty()
Definition: ctpl_stl.h:75
std::function< void(int)> pop()
Definition: ctpl_stl.h:145
thread_pool()
Definition: ctpl_stl.h:88
std::thread & get_thread(int i)
Definition: ctpl_stl.h:102
void stop(bool isWait=false)
Definition: ctpl_stl.h:161
int size()
Definition: ctpl_stl.h:98
auto push(F &&f) -> std::future< decltype(f(0))>
Definition: ctpl_stl.h:213
~thread_pool()
Definition: ctpl_stl.h:95