std::future<typename std::result_of<TFunction()>::type> submit(TFunction&& func) { typedef typename std::result_of<TFunction()>::type result_type; std::packaged_task<result_type()> task(std::forward<TFunction>(func)); std::future<result_type> future_result(task.get_future()); m_work_queue.push(std::move(task)); return future_result; }
void worker_thread() { while (!m_done) { std::function<void()> task; if (m_work_queue.try_pop(task)) { task(); } else { std::this_thread::yield(); } } }
void worker_thread() { osmium::thread::set_thread_name("_osmium_worker"); while (true) { function_wrapper task; m_work_queue.wait_and_pop_with_timeout(task); if (task) { if (task()) { // The called tasks returns true only when the // worker thread should shut down. return; } } } }
bool queue_empty() const { return m_work_queue.empty(); }
size_t queue_size() const { return m_work_queue.size(); }
~Pool() { shutdown_all_workers(); m_work_queue.shutdown(); }
void shutdown_all_workers() { for (int i = 0; i < m_num_threads; ++i) { // The special function wrapper makes a worker shut down. m_work_queue.push(function_wrapper{0}); } }
inline void add_to_queue(osmium::thread::Queue<std::future<T>>& queue, std::exception_ptr&& exception) { std::promise<T> promise; queue.push(promise.get_future()); promise.set_exception(std::move(exception)); }
inline void add_to_queue(osmium::thread::Queue<std::future<T>>& queue, T&& data) { std::promise<T> promise; queue.push(promise.get_future()); promise.set_value(std::forward<T>(data)); }
size_t submit(TFunction f) { return m_work_queue.push_and_get_size(std::function<void()>(f)); }