7 #include <condition_variable>
14 #include <type_traits>
25 struct sched_param thread_param;
26 thread_param.sched_priority = phy_prio;
29 spdlog::info(
"Launching phy thread with realtime scheduling priority {}", thread_param.sched_priority );
32 int error = pthread_setschedparam(
m_workers.back().native_handle(), SCHED_RR, &thread_param );
35 spdlog::error(
"Cannot set phy thread priority to realtime: {}. Thread will run at default priority.", strerror(error));
54 template <
class Func,
class... Args>
55 auto push(Func &&fn, Args &&...args)
57 using return_type =
typename std::result_of<Func(Args...)>::type;
59 auto task{ std::make_shared<std::packaged_task<return_type()>>(
60 std::bind(std::forward<Func>(fn), std::forward<Args>(args)...)
63 auto future{ task->get_future() };
64 std::unique_lock<std::mutex> lock{
m_mutex };
78 std::unique_lock<std::mutex> lock{
m_mutex };
92 if (thread.joinable()) {
135 std::unique_lock<std::mutex> lock{
m_mutex };
thread_pool(thread_pool &&)=default
thread_pool & operator=(thread_pool &&)=default
std::atomic< std::size_t > m_active
std::size_t thread_count() const
std::vector< std::thread > m_workers
std::condition_variable m_notifier
std::size_t active_count() const
thread_pool & operator=(thread_pool const &)=delete
std::queue< task_type > m_tasks
thread_pool(std::size_t thread_count=std::thread::hardware_concurrency(), int phy_prio=10)
auto push(Func &&fn, Args &&...args)
std::function< void()> task_type
thread_pool(thread_pool const &)=delete
std::atomic< bool > m_stop