TARAXA
thread_pool.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/asio.hpp>
4 
5 #include "common/functional.hpp"
6 
7 namespace taraxa::util {
8 
9 class ThreadPool : std::enable_shared_from_this<ThreadPool> {
10  using asio_callback = std::function<void(boost::system::error_code const &)>;
11 
12  boost::asio::io_context ioc_;
13  boost::asio::executor_work_guard<decltype(ioc_)::executor_type> ioc_work_;
14  std::vector<std::thread> threads_;
15 
16  std::atomic<uint64_t> num_pending_tasks_ = 0;
17 
18  public:
19  explicit ThreadPool(size_t num_threads = std::thread::hardware_concurrency(), bool _start = true);
20  ~ThreadPool();
21 
22  ThreadPool(ThreadPool const &) = delete;
23  ThreadPool &operator=(ThreadPool const &) = delete;
24  ThreadPool(ThreadPool &&) = delete;
26 
27  auto capacity() const { return threads_.capacity(); }
28  uint64_t num_pending_tasks() const { return num_pending_tasks_; }
29 
30  // TODO eliminate
31  auto &unsafe_get_io_context() { return ioc_; }
32 
33  void start();
34  bool is_running() const;
35  void stop();
36 
37  void post(uint64_t do_in_ms, asio_callback action);
38  void post(uint64_t err, std::function<void()> action);
39 
40  template <typename Action>
41  auto post(Action &&action) {
42  return post(0, std::forward<Action>(action));
43  }
44 
45  struct Periodicity {
46  uint64_t period_ms = 0, delay_ms = period_ms;
47  };
48  void post_loop(Periodicity const &periodicity, std::function<void()> action);
49 
50  operator task_executor_t() {
51  return [this](auto &&task) { post(std::forward<task_t>(task)); };
52  }
53 };
54 
55 } // namespace taraxa::util
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