23#include <condition_variable>
55 workers_.emplace_back([
this] { worker_loop(); });
61 std::lock_guard<std::mutex>
lock(queue_mutex_);
64 condition_.notify_all();
65 for (
auto&
w : workers_) {
85 -> std::future<std::invoke_result_t<std::decay_t<F>>> {
86 using return_type = std::invoke_result_t<std::decay_t<F>>;
89 = std::make_shared<std::packaged_task<
return_type()>>(std::forward<F>(
task));
93 std::lock_guard<std::mutex>
lock(queue_mutex_);
95 throw std::runtime_error(
"enqueue on stopped thread_pool");
97 tasks_.emplace([
packaged]() { (*packaged)(); });
99 condition_.notify_one();
108 std::unique_lock<std::mutex>
lock(queue_mutex_);
109 condition_.wait(
lock, [
this] {
return stop_ || !tasks_.empty(); });
110 if (stop_ && tasks_.empty()) {
113 task = std::move(tasks_.front());
120 std::vector<std::thread> workers_;
121 std::queue<std::function<
void()>> tasks_;
122 std::mutex queue_mutex_;
123 std::condition_variable condition_;
Read-only map of maps of maps (view into a dict-of-dict-of-dict structure)
Definition coreviews.hpp:109
Command pattern: tasks are encapsulated as command objects (std::function<void()>) placed in a shared...
Definition thread_pool.hpp:42
~thread_pool()
Definition thread_pool.hpp:59
thread_pool(size_t num_threads=std::thread::hardware_concurrency())
Definition thread_pool.hpp:48
thread_pool(thread_pool &&)=delete
thread_pool & operator=(const thread_pool &)=delete
thread_pool(const thread_pool &)=delete
thread_pool & operator=(thread_pool &&)=delete
auto enqueue(F &&task) -> std::future< std::invoke_result_t< std::decay_t< F > > >
Definition thread_pool.hpp:84
Definition digraphs.hpp:24