31 #ifndef GRT_THREAD_POOL_HEADER
32 #define GRT_THREAD_POOL_HEADER
39 #ifdef GRT_CXX11_ENABLED
43 #include <condition_variable>
46 #endif //GRT_CXX11_ENABLED
70 #ifdef GRT_CXX11_ENABLED
81 template<
class F,
class... Args>
82 auto enqueue(F&& f, Args&&... args) -> std::future<
typename std::result_of<F(Args...) >::type>;
83 #endif //GRT_CXX11_ENABLED
102 #ifdef GRT_CXX11_ENABLED
103 void launchThreads(
const unsigned int threads);
105 std::vector< std::thread > workers;
106 std::queue< std::function<void()> > tasks;
109 std::mutex queue_mutex;
110 std::condition_variable condition;
111 std::atomic< bool > stop;
113 static std::atomic< unsigned int > threadPoolSize;
114 #endif //GRT_CXX11_ENABLED
117 #ifdef GRT_CXX11_ENABLED
119 template<
class F,
class... Args>
auto ThreadPool::enqueue(F&& func, Args&&... args) -> std::future<
typename std::result_of<F(Args...)>::type>
122 using return_type =
typename std::result_of< F(Args...) >::type;
124 auto task = std::make_shared< std::packaged_task< return_type() > >( std::bind(std::forward<F>( func ), std::forward<Args>(args)...) );
126 std::future< return_type > res = task->get_future();
128 std::unique_lock< std::mutex > lock( queue_mutex );
132 throw std::runtime_error(
"enqueue on stopped ThreadPool");
134 tasks.emplace( [task](){ (*task)(); } );
136 condition.notify_one();
139 #endif //GRT_CXX11_ENABLED
143 #endif //GRT_THREAD_POOL_HEADER
static unsigned int getThreadPoolSize()
static bool setThreadPoolSize(const unsigned int threadPoolSize)