3 #include <boost/asio.hpp>
9 class ThreadPool : std::enable_shared_from_this<ThreadPool> {
10 using asio_callback = std::function<void(boost::system::error_code
const &)>;
12 boost::asio::io_context
ioc_;
13 boost::asio::executor_work_guard<decltype(
ioc_)::executor_type>
ioc_work_;
19 explicit ThreadPool(
size_t num_threads = std::thread::hardware_concurrency(),
bool _start =
true);
38 void post(uint64_t err, std::function<
void()> action);
40 template <
typename Action>
41 auto post(Action &&action) {
42 return post(0, std::forward<Action>(action));
51 return [
this](
auto &&task) {
post(std::forward<task_t>(task)); };
Definition: thread_pool.hpp:9
uint64_t num_pending_tasks() const
Definition: thread_pool.hpp:28
void post(uint64_t do_in_ms, asio_callback action)
Definition: thread_pool.cpp:36
std::atomic< uint64_t > num_pending_tasks_
Definition: thread_pool.hpp:16
ThreadPool(ThreadPool const &)=delete
~ThreadPool()
Definition: thread_pool.cpp:70
ThreadPool(size_t num_threads=std::thread::hardware_concurrency(), bool _start=true)
Definition: thread_pool.cpp:5
ThreadPool & operator=(ThreadPool const &)=delete
void stop()
Definition: thread_pool.cpp:25
boost::asio::io_context ioc_
Definition: thread_pool.hpp:12
bool is_running() const
Definition: thread_pool.cpp:23
auto post(Action &&action)
Definition: thread_pool.hpp:41
boost::asio::executor_work_guard< decltype(ioc_)::executor_type > ioc_work_
Definition: thread_pool.hpp:13
uint64_t delay_ms
Definition: thread_pool.hpp:46
ThreadPool(ThreadPool &&)=delete
std::function< void(boost::system::error_code const &)> asio_callback
Definition: thread_pool.hpp:10
std::vector< std::thread > threads_
Definition: thread_pool.hpp:14
auto & unsafe_get_io_context()
Definition: thread_pool.hpp:31
void post_loop(Periodicity const &periodicity, std::function< void()> action)
Definition: thread_pool.cpp:63
ThreadPool & operator=(ThreadPool &&)=delete
auto capacity() const
Definition: thread_pool.hpp:27
uint64_t period_ms
Definition: thread_pool.hpp:46
void start()
Definition: thread_pool.cpp:14
Definition: thread_pool.hpp:45
Definition: default_construct_copyable_movable.hpp:10
std::function< void(task_t &&)> task_executor_t
Definition: functional.hpp:7