void p3(boost::shared_future<int> f) { BOOST_THREAD_LOG << "p3 <" << &f << BOOST_THREAD_END_LOG; BOOST_TEST(f.valid()); int i = f.get(); boost::this_thread::sleep_for(boost::chrono::milliseconds(500)); BOOST_THREAD_LOG << "p3 <" << &f << " " << i << BOOST_THREAD_END_LOG; return ; }
static void run(boost::shared_ptr<queue_type> const& queue,boost::shared_ptr<diag_type> diagnostics, boost::shared_ptr<boost::asynchronous::lockfree_queue<boost::asynchronous::any_callable> > const& private_queue, boost::shared_future<boost::thread*> self, boost::weak_ptr<this_type> this_) { boost::thread* t = self.get(); boost::asynchronous::detail::single_queue_scheduler_policy<Q>::m_self_thread.reset(new thread_ptr_wrapper(t)); // thread scheduler => tss boost::asynchronous::any_weak_scheduler<job_type> self_as_weak = boost::asynchronous::detail::lockable_weak_scheduler<this_type>(this_); boost::asynchronous::get_thread_scheduler<job_type>(self_as_weak,true); std::list<boost::asynchronous::any_continuation>& waiting = boost::asynchronous::get_continuations(std::list<boost::asynchronous::any_continuation>(),true); CPULoad cpu_load; while(true) { // get a job typename Q::job_type job; try { { bool popped = execute_one_job(queue,cpu_load,diagnostics,waiting); if (!popped) { cpu_load.loop_done_no_job(); // nothing for us to do, give up our time slice boost::this_thread::yield(); } // check for shutdown boost::asynchronous::any_callable djob; popped = private_queue->try_pop(djob); if (popped) { djob(); } } // job destroyed (for destruction useful) // check if we got an interruption job boost::this_thread::interruption_point(); } catch(boost::asynchronous::detail::shutdown_exception&) { // we are done, execute jobs posted short before to the end, then shutdown while(execute_one_job(queue,cpu_load,diagnostics,waiting)); delete boost::asynchronous::detail::single_queue_scheduler_policy<Q>::m_self_thread.release(); return; } catch(boost::thread_interrupted&) { // task interrupted, no problem, just continue } catch(std::exception&) { boost::asynchronous::job_traits<typename Q::job_type>::set_failed(job); } } }
int p2s(boost::shared_future<int>& f) { BOOST_THREAD_LOG << "<P2" << BOOST_THREAD_END_LOG; try { return 2 * f.get(); } catch (std::exception& ex) { BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; BOOST_ASSERT(false); } catch (...) { BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; BOOST_ASSERT(false); } BOOST_THREAD_LOG << "P2>" << BOOST_THREAD_END_LOG; }
void interrupt(boost::shared_future<boost::thread*> fworker) { boost::mutex::scoped_lock lock(m_mutex); // if task is already interrupted, no nothing // if task is completed, ditto if (is_completed_int()) { // mark that we are interrupted m_state = 2; // no need to protect m_state any more lock.unlock(); // copy to minimize locking boost::mutex::scoped_lock ilock(m_interruptibles_mutex); std::vector<boost::asynchronous::any_interruptible> interruptibles = m_interruptibles; ilock.unlock(); // interrupt subs if any for (std::vector<boost::asynchronous::any_interruptible>::iterator it = interruptibles.begin(); it != interruptibles.end();++it) { (*it).interrupt(); } return; } if (is_interrupted_int()) { return; } else if (is_running_int()) { // copy to minimize locking boost::mutex::scoped_lock ilock(m_interruptibles_mutex); std::vector<boost::asynchronous::any_interruptible> interruptibles = m_interruptibles; ilock.unlock(); // interrupt subs if any for (std::vector<boost::asynchronous::any_interruptible>::iterator it = interruptibles.begin(); it != interruptibles.end();++it) { (*it).interrupt(); } // if true then we have a worker boost::thread* w = fworker.get(); w->interrupt(); } m_state = 2; }
bool is_failed(boost::shared_future<bool> future) { return future.get() == false; }