71 : _workers(), _tasks(), _mtx(), _cv(), _run(false)
84 std::lock_guard<std::mutex> lock(_mtx);
85 if (_workers.empty()) {
87 for (
size_t i = 0; i < N; ++i) {
88 _workers.emplace_back(std::thread(&ThreadPool::WorkerThread,
this));
102 std::lock_guard<std::mutex> lock(_mtx);
103 _tasks.emplace_back(Task(fn,std::make_tuple(args...)));
116 std::lock_guard<std::mutex> lock(_mtx);
120 for (std::thread & worker : _workers) {
124 std::lock_guard<std::mutex> lock(_mtx);
137 std::lock_guard<std::mutex> lock(_mtx);
162 Task(
const Task & t) =
default;
163 Task(Task && t) =
default;
164 Task(F f, std::tuple<Args...> && a) : _fn(f), _args(a) { }
165 Task & operator = (
const Task &) =
default;
166 Task & operator = (Task &&) =
default;
172 inline void Execute()
174 return CallFunc(std::index_sequence_for<Args...>());
179 std::tuple<Args...> _args;
184 template<
size_t ...S>
185 inline void CallFunc(std::index_sequence<S...>)
187 _fn(std::get<S>(_args) ...);
192 std::vector<std::thread> _workers;
193 std::deque<Task> _tasks;
195 std::condition_variable _cv;
206 [
this] {
return ((! this->_run) || (! this->_tasks.empty())); };
211 std::unique_lock<std::mutex> lock(this->_mtx);
212 this->_cv.wait(lock, waitFor);
213 if ((! this->_run) && this->_tasks.empty()) {
218 task = this->_tasks.front();
219 this->_tasks.pop_front();
void AddTask(F fn, Args ...args)
Adds a task to be executed by the thread pool.
Definition DwmThreadPool.hh:99