30 #include <condition_variable> 34 #include <unordered_set> 46 template <
typename Closure>
73 template <
typename... ArgsT>
105 void _spawn(
unsigned);
110 template <
typename Closure>
116 template <
typename Closure>
121 template <
typename Closure>
123 return _threads.size();
127 template <
typename Closure>
135 template <
typename Closure>
140 for(
size_t i=0; i<N; ++i) {
142 _threads.emplace_back([
this] () ->
void {
150 if(!_tasks.empty()) {
151 task = std::move(_tasks.back());
160 while(_tasks.empty() && !_stop) {
161 _worker_signal.wait(lock);
172 template <
typename Closure>
173 template <
typename... ArgsT>
177 if(num_workers() == 0) {
178 Closure{std::forward<ArgsT>(args)...}();
182 std::scoped_lock lock(_mutex);
183 _tasks.emplace_back(std::forward<ArgsT>(args)...);
184 _worker_signal.notify_one();
190 template <
typename Closure>
194 if(num_workers() == 0) {
202 bool notify_all = tasks.size() > 1;
204 std::scoped_lock lock(_mutex);
205 _tasks.reserve(_tasks.size() + tasks.size());
206 std::move(tasks.begin(), tasks.end(), std::back_inserter(_tasks));
209 _worker_signal.notify_all();
212 _worker_signal.notify_one();
220 template <
typename Closure>
226 std::scoped_lock lock(_mutex);
228 _worker_signal.notify_all();
231 for(
auto& t : _threads) {
void batch(std::vector< Closure > &&closures)
moves a batch of closures to the executor
Definition: simple_threadpool.hpp:191
Definition: taskflow.hpp:6
size_t num_workers() const
queries the number of worker threads
Definition: simple_threadpool.hpp:122
Executor that implements a centralized task queue with a simple execution strategy.
Definition: simple_threadpool.hpp:47
bool is_owner() const
queries if the caller is the owner of the executor
Definition: simple_threadpool.hpp:128
void emplace(ArgsT &&... args)
constructs the closure in place in the executor
Definition: simple_threadpool.hpp:174
SimpleThreadpool(unsigned N)
constructs the executor with a given number of worker threads
Definition: simple_threadpool.hpp:111
~SimpleThreadpool()
destructs the executor
Definition: simple_threadpool.hpp:117