1#ifndef BITSIM_UTILS_CONCURRENCY_THREAD_POOL_H
2#define BITSIM_UTILS_CONCURRENCY_THREAD_POOL_H
7#include <boost/noncopyable.hpp>
68 template <typename F, typename... Args>
69 auto
submit(F &&f, Args &&...args) -> std::future<std::invoke_result_t<F, Args...>>;
75 std::vector<std::thread> workers_;
84template <typename F, typename... Args>
85auto
ThreadPool::
submit(F &&f, Args &&...args) -> std::future<std::invoke_result_t<F, Args...>>
87 using result_type = std::invoke_result_t<F, Args...>;
90 auto task = std::make_shared<std::packaged_task<result_type()>>(
91 [f = std::forward<F>(f), ... args = std::forward<Args>(args)]()
mutable -> result_type
92 {
return std::invoke(std::move(f), std::move(args)...); });
94 auto result = task->get_future();
95 if (!tasks_.push([task]() { (*task)(); }))
97 throw std::logic_error(
"ThreadPool: submit() on a pool that is shutting down");
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
A first-in first-out queue that several threads can push to and pop from.
Definition concurrent_queue.h:26
A fixed number of threads that run the tasks given to them, in the order given.
Definition thread_pool.h:33
~ThreadPool()
Run the tasks still queued, then join the threads.
Definition thread_pool.cpp:51
uint_t size() const noexcept
The number of threads.
Definition thread_pool.h:54
bool is_worker_thread() const noexcept
Whether the calling thread is one of this pool's threads.
Definition thread_pool.cpp:64
ThreadPool(uint_t n_threads=0)
Start n_threads threads; 0 means default_n_threads().
Definition thread_pool.cpp:38
auto submit(F &&f, Args &&...args) -> std::future< std::invoke_result_t< F, Args... > >
Queue f(args...) to run on one of the threads.
Definition thread_pool.h:85
static uint_t default_n_threads()
The number of threads to use when none is given: the environment variable BITSIM_NUM_THREADS if it is...
Definition thread_pool.cpp:17
Definition concurrent_queue.h:15
ThreadPool & default_thread_pool()
The pool that parallel_for() and parallel_map() use when given none. It starts ThreadPool::default_n_...
Definition thread_pool.cpp:77
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40