27 #include <condition_variable> 30 #include <unordered_set> 42 template <
typename Closure>
69 template <
typename... ArgsT>
89 size_t num_tasks()
const;
103 void _spawn(
unsigned);
108 template <
typename Closure>
114 template <
typename Closure>
124 template <
typename Closure>
126 return _tasks.size();
129 template <
typename Closure>
131 return _threads.size();
135 template <
typename Closure>
143 template <
typename Closure>
148 for(
size_t i=0; i<N; ++i) {
150 _threads.emplace_back([
this] () ->
void {
158 if(!_tasks.empty()) {
159 task = std::move(_tasks.back());
168 while(_tasks.empty() && !_stop) {
169 _worker_signal.wait(lock);
180 template <
typename Closure>
181 template <
typename... ArgsT>
186 Closure{std::forward<ArgsT>(args)...}();
190 std::scoped_lock lock(_mutex);
191 _tasks.emplace_back(std::forward<ArgsT>(args)...);
192 _worker_signal.notify_one();
198 template <
typename Closure>
210 bool notify_all = tasks.size() > 1;
212 std::scoped_lock lock(_mutex);
213 _tasks.reserve(_tasks.size() + tasks.size());
214 std::move(tasks.begin(), tasks.end(), std::back_inserter(_tasks));
217 _worker_signal.notify_all();
220 _worker_signal.notify_one();
228 template <
typename Closure>
234 std::scoped_lock lock(_mutex);
236 _worker_signal.notify_all();
239 for(
auto& t : _threads) {
void batch(std::vector< Closure > &&closures)
moves a batch of closures to the executor
Definition: simple_threadpool.hpp:199
Definition: taskflow.hpp:6
size_t num_workers() const
queries the number of worker threads
Definition: simple_threadpool.hpp:130
Executor that implements a centralized task queue with a simple execution strategy.
Definition: simple_threadpool.hpp:43
bool is_owner() const
queries if the caller is the owner of the executor
Definition: simple_threadpool.hpp:136
void emplace(ArgsT &&... args)
constructs the closure in place in the executor
Definition: simple_threadpool.hpp:182
SimpleThreadpool(unsigned N)
constructs the executor with a given number of worker threads
Definition: simple_threadpool.hpp:109
~SimpleThreadpool()
destructs the executor
Definition: simple_threadpool.hpp:115