
On Wed, 14 Mar 2007 06:49:57 +0100, Oliver.Kowalke wrote:
I've made a futures implementation in the hopes of combining http://braddock.com/~braddock/future/
In your implementation I'm missing operators for logical combination of futures like it is supported by the futures implementation of Thorsten Schütt (vault files).
Hi Oliver, Yes, I would like to either add this, or merge into Schutt's implementation if he is interested. And example of an unintrusive operator||() is below. I added a public add_callback() method which calls an observer function when the future's state changes. This should allow grouping of futures or combined waits to be handled unintrusively outside of the main future classes. This public observer method is also needed for things like future-aware task scheduling (like Frank Hess's draft active object or boost::coroutine). template <typename T> struct future_or { future_or(future<T> &a, future<T> &b) : a_(a), b_(b) {} future_or(const future_or &f) : a_(f.a_), b_(f.b_), p_(f.p_) {} struct future_or_failed : public std::exception { }; void operator()() { boost::mutex::scoped_lock lck(mutex_); if (a_.has_value()) p_.set(a_.get()); else if (b_.has_value()) p_.set(b_.get()); else if (a_.has_exception() && b_.has_exception()) // both failed p_.set_exception(future_or_failed()); } future<T> a_,b_; promise<T> p_; boost::mutex mutex_; }; template<typename T> future<T> operator||(future<T> &a, future<T> &b) { future_or<T> fa(a, b); a.add_callback(fa); return fa.p_; } void TestCase7() { // test future or || example promise<int> p1; promise<int> p2; future<int> f1(p1); future<int> f2(p2); future<int> f1_or_f2 = f1 || f2; BOOST_CHECK(!f1_or_f2.ready()); p1.set(97); p2.set(-5); // will be ignored by the or BOOST_CHECK(f1_or_f2.ready()); BOOST_CHECK_EQUAL(f1_or_f2.get(), 97); }