bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
thread_pool.h
Go to the documentation of this file.
1#ifndef BITSIM_UTILS_CONCURRENCY_THREAD_POOL_H
2#define BITSIM_UTILS_CONCURRENCY_THREAD_POOL_H
3
6
7#include <boost/noncopyable.hpp>
8
9#include <functional>
10#include <future>
11#include <memory>
12#include <stdexcept>
13#include <thread>
14#include <type_traits>
15#include <utility>
16#include <vector>
17
18namespace bitsim
19{
20namespace utils::concurrency
21{
22
32class ThreadPool : private boost::noncopyable
33{
34 public:
40
44 explicit ThreadPool(uint_t n_threads = 0);
45
50
54 uint_t size() const noexcept { return static_cast<uint_t>(workers_.size()); }
55
62 bool is_worker_thread() const noexcept;
63
68 template <typename F, typename... Args>
69 auto submit(F &&f, Args &&...args) -> std::future<std::invoke_result_t<F, Args...>>;
70
71 private:
72 void work_();
73
74 ConcurrentQueue<std::function<void()>> tasks_;
75 std::vector<std::thread> workers_;
76};
77
83
84template <typename F, typename... Args>
85auto ThreadPool::submit(F &&f, Args &&...args) -> std::future<std::invoke_result_t<F, Args...>>
86{
87 using result_type = std::invoke_result_t<F, Args...>;
88
89 // std::function needs a copyable callable, and a packaged_task is not
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)...); });
93
94 auto result = task->get_future();
95 if (!tasks_.push([task]() { (*task)(); }))
96 {
97 throw std::logic_error("ThreadPool: submit() on a pool that is shutting down");
98 }
99 return result;
100}
101
102} // namespace utils::concurrency
103} // namespace bitsim
104
105#endif // BITSIM_UTILS_CONCURRENCY_THREAD_POOL_H
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