23#include <condition_variable>
48 workers_.emplace_back([
this] { worker_loop(); });
54 std::lock_guard<std::mutex>
lock(queue_mutex_);
57 condition_.notify_all();
58 for (
auto&
w : workers_) {
78 -> std::future<std::invoke_result_t<std::decay_t<F>>> {
79 using return_type = std::invoke_result_t<std::decay_t<F>>;
82 = std::make_shared<std::packaged_task<
return_type()>>(std::forward<F>(
task));
86 std::lock_guard<std::mutex>
lock(queue_mutex_);
88 throw std::runtime_error(
"enqueue on stopped thread_pool");
90 tasks_.emplace([
packaged]() { (*packaged)(); });
92 condition_.notify_one();
101 std::unique_lock<std::mutex>
lock(queue_mutex_);
102 condition_.wait(
lock, [
this] {
return stop_ || !tasks_.empty(); });
103 if (stop_ && tasks_.empty()) {
106 task = std::move(tasks_.front());
113 std::vector<std::thread> workers_;
114 std::queue<std::function<
void()>> tasks_;
115 std::mutex queue_mutex_;
116 std::condition_variable condition_;
Read-only map of maps of maps (view into a dict-of-dict-of-dict structure)
Definition coreviews.hpp:109
Definition thread_pool.hpp:35
~thread_pool()
Definition thread_pool.hpp:52
thread_pool(size_t num_threads=std::thread::hardware_concurrency())
Definition thread_pool.hpp:41
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:77
Definition digraphs.hpp:24