| 1 |
// https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h |
| 2 |
// |
| 3 |
// modified so that a thread_id integer in [0..nb_threads] is passed to each task |
| 4 |
// |
| 5 |
#ifndef THREAD_POOL_H |
| 6 |
#define THREAD_POOL_H |
| 7 |
|
| 8 |
#include <vector> |
| 9 |
#include <queue> |
| 10 |
#include <memory> |
| 11 |
#include <thread> |
| 12 |
#include <mutex> |
| 13 |
#include <condition_variable> |
| 14 |
#include <future> |
| 15 |
#include <functional> |
| 16 |
#include <stdexcept> |
| 17 |
|
| 18 |
class ThreadPool { |
| 19 |
public: |
| 20 |
ThreadPool(size_t); |
| 21 |
template<class F, class... Args> |
| 22 |
auto enqueue(F&& f, Args&&... args) |
| 23 |
-> std::future<typename std::result_of<F(int, Args...)>::type>; |
| 24 |
//~ThreadPool(); |
| 25 |
void join(); |
| 26 |
private: |
| 27 |
// need to keep track of threads so we can join them |
| 28 |
std::vector< std::thread > workers; |
| 29 |
// the task queue |
| 30 |
std::queue< std::function<void(int)> > tasks; |
| 31 |
|
| 32 |
// synchronization |
| 33 |
std::mutex queue_mutex; |
| 34 |
std::condition_variable condition; |
| 35 |
bool stop; |
| 36 |
}; |
| 37 |
|
| 38 |
// the constructor just launches some amount of workers |
| 39 |
inline ThreadPool::ThreadPool(size_t threads) |
| 40 |
: stop(false) |
| 41 |
{ |
| 42 |
for(size_t thread_id = 0; thread_id<threads; ++ thread_id) |
| 43 |
workers.emplace_back( |
| 44 |
[this, thread_id] |
| 45 |
{ |
| 46 |
for(;;) |
| 47 |
{ |
| 48 |
std::function<void(int)> task; |
| 49 |
|
| 50 |
{ |
| 51 |
std::unique_lock<std::mutex> lock(this->queue_mutex); |
| 52 |
this->condition.wait(lock, |
| 53 |
[this]{ return this->stop || !this->tasks.empty(); }); |
| 54 |
if(this->stop && this->tasks.empty()) |
| 55 |
return; |
| 56 |
task = std::move(this->tasks.front()); |
| 57 |
this->tasks.pop(); |
| 58 |
} |
| 59 |
|
| 60 |
task(thread_id); |
| 61 |
} |
| 62 |
} |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
// add new work item to the pool |
| 67 |
template<class F, class... Args> |
| 68 |
auto ThreadPool::enqueue(F&& f, Args&&... args) |
| 69 |
-> std::future<typename std::result_of<F(int, Args...)>::type> |
| 70 |
{ |
| 71 |
using return_type = typename std::result_of<F(int, Args...)>::type; |
| 72 |
|
| 73 |
auto task = std::make_shared< std::packaged_task<return_type(int)> >( |
| 74 |
std::bind(std::forward<F>(f), placeholders::_1, std::forward<Args>(args)...) |
| 75 |
); |
| 76 |
|
| 77 |
std::future<return_type> res = task->get_future(); |
| 78 |
{ |
| 79 |
std::unique_lock<std::mutex> lock(queue_mutex); |
| 80 |
|
| 81 |
// don't allow enqueueing after stopping the pool |
| 82 |
if(stop) |
| 83 |
throw std::runtime_error("enqueue on stopped ThreadPool"); |
| 84 |
|
| 85 |
tasks.emplace([task](int thread_id){ (*task)(thread_id); }); |
| 86 |
} |
| 87 |
condition.notify_one(); |
| 88 |
return res; |
| 89 |
} |
| 90 |
|
| 91 |
// the destructor joins all threads |
| 92 |
// rayan: slightly modified, now explicit join |
| 93 |
void ThreadPool::join() |
| 94 |
{ |
| 95 |
{ |
| 96 |
std::unique_lock<std::mutex> lock(queue_mutex); |
| 97 |
stop = true; |
| 98 |
} |
| 99 |
condition.notify_all(); |
| 100 |
for(std::thread &worker: workers) |
| 101 |
worker.join(); |
| 102 |
} |
| 103 |
|
| 104 |
#endif |