9 #include "fwThread/config.hpp" 11 #include <fwCore/base.hpp> 13 #include <condition_variable> 32 class FWTHREAD_CLASS_API
Pool 35 typedef std::shared_ptr<Pool> sptr;
40 FWTHREAD_API Pool(
size_t);
45 template<
class F,
class ... Args>
46 auto post(F&& f, Args&& ... args)
47 ->std::shared_future<
typename std::result_of<F(Args ...)>::type>;
51 std::vector< std::thread > m_workers;
54 std::queue< std::function<void()> > m_tasks;
57 std::mutex m_queueMutex;
58 std::condition_variable m_condition;
64 template<
class F,
class ... Args>
66 ->std::shared_future<
typename std::result_of<F(Args ...)>::type>
68 using return_type =
typename std::result_of<F(Args ...)>::type;
70 auto task = std::make_shared< std::packaged_task<return_type()> >(
71 std::bind(std::forward<F>(f), std::forward<Args>(args) ...)
74 std::shared_future<return_type> res = task->get_future();
76 std::unique_lock<std::mutex> lock(m_queueMutex);
81 throw std::runtime_error(
"enqueue on stopped Pool");
84 m_tasks.emplace([task]()
89 m_condition.notify_one();
FWTHREAD_API Pool & getDefaultPool()
Get the default pool.
auto post(F &&f, Args &&...args) -> std::shared_future< typename std::result_of< F(Args...)>::type >
add new work item to the pool
This class creates and manages a pool of threads which process tasks.
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...