#include #include #include typedef boost::error_info err_info; class err : public boost::exception , public std::exception { }; class future { public: future () : ready_ (false) {} void set_exception (boost::exception_ptr const& e) { boost::unique_lock lck (mux_); exc_ = e; ready_ = true; cond_.notify_all(); } void get_exception () const { boost::unique_lock lck (mux_); while (! ready_) cond_.wait (lck); rethrow_exception (exc_); } private: bool ready_; boost::exception_ptr exc_; mutable boost::mutex mux_; mutable boost::condition_variable cond_; }; void producer (future& f) { f.set_exception (boost::copy_exception (err () << err_info ("stub"))); } void consumer () { future f; boost::thread thr (boost::bind (&producer, boost::ref (f))); try { f.get_exception (); } catch (err const& e) {} thr.join (); } void consume () { for (int i=0;i<100;++i) consumer (); } int main () { boost::thread_group grp; for (int i=0; i<50; ++i) grp.create_thread (&consume); grp.join_all (); }