$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r83676 - in trunk: boost/thread libs/thread/example libs/thread/test libs/thread/test/sync/futures/future
From: vicente.botet_at_[hidden]
Date: 2013-04-01 12:17:07
Author: viboes
Date: 2013-04-01 12:17:05 EDT (Mon, 01 Apr 2013)
New Revision: 83676
URL: http://svn.boost.org/trac/boost/changeset/83676
Log:
Thread: major rewrite of future::then.
Text files modified: 
   trunk/boost/thread/future.hpp                            |   941 +++++++++++++++++++++++++-------------- 
   trunk/libs/thread/example/future_then.cpp                |     5                                         
   trunk/libs/thread/test/Jamfile.v2                        |     4                                         
   trunk/libs/thread/test/sync/futures/future/then_pass.cpp |    82 +-                                      
   4 files changed, 653 insertions(+), 379 deletions(-)
Modified: trunk/boost/thread/future.hpp
==============================================================================
--- trunk/boost/thread/future.hpp	(original)
+++ trunk/boost/thread/future.hpp	2013-04-01 12:17:05 EDT (Mon, 01 Apr 2013)
@@ -1,5 +1,5 @@
 //  (C) Copyright 2008-10 Anthony Williams
-//  (C) Copyright 2011-2012 Vicente J. Botet Escriba
+//  (C) Copyright 2011-2013 Vicente J. Botet Escriba
 //
 //  Distributed under the Boost Software License, Version 1.0. (See
 //  accompanying file LICENSE_1_0.txt or copy at
@@ -67,41 +67,41 @@
 namespace boost
 {
 
-    //enum class launch
-    BOOST_SCOPED_ENUM_DECLARE_BEGIN(launch)
-    {
-        none = 0,
-        async = 1,
-        deferred = 2,
-        any = async | deferred
-    }
-    BOOST_SCOPED_ENUM_DECLARE_END(launch)
+  //enum class launch
+  BOOST_SCOPED_ENUM_DECLARE_BEGIN(launch)
+  {
+      none = 0,
+      async = 1,
+      deferred = 2,
+      any = async | deferred
+  }
+  BOOST_SCOPED_ENUM_DECLARE_END(launch)
 
-    //enum class future_status
-    BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_status)
-    {
-        ready,
-        timeout,
-        deferred
-    }
-    BOOST_SCOPED_ENUM_DECLARE_END(future_status)
+  //enum class future_status
+  BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_status)
+  {
+      ready,
+      timeout,
+      deferred
+  }
+  BOOST_SCOPED_ENUM_DECLARE_END(future_status)
 
-    class BOOST_SYMBOL_VISIBLE future_error
-        : public std::logic_error
-    {
-        system::error_code ec_;
-    public:
-        future_error(system::error_code ec)
-        : logic_error(ec.message()),
-          ec_(ec)
-        {
-        }
+  class BOOST_SYMBOL_VISIBLE future_error
+      : public std::logic_error
+  {
+      system::error_code ec_;
+  public:
+      future_error(system::error_code ec)
+      : logic_error(ec.message()),
+        ec_(ec)
+      {
+      }
 
-        const system::error_code& code() const BOOST_NOEXCEPT
-        {
-          return ec_;
-        }
-    };
+      const system::error_code& code() const BOOST_NOEXCEPT
+      {
+        return ec_;
+      }
+  };
 
     class BOOST_SYMBOL_VISIBLE future_uninitialized:
         public future_error
@@ -145,23 +145,23 @@
         {}
     };
 
-    class BOOST_SYMBOL_VISIBLE task_moved:
-        public future_error
-    {
-    public:
-        task_moved():
-          future_error(system::make_error_code(future_errc::no_state))
-        {}
-    };
+        class BOOST_SYMBOL_VISIBLE task_moved:
+            public future_error
+        {
+        public:
+            task_moved():
+              future_error(system::make_error_code(future_errc::no_state))
+            {}
+        };
 
-    class promise_moved:
-        public future_error
-    {
-    public:
-          promise_moved():
-          future_error(system::make_error_code(future_errc::no_state))
-        {}
-    };
+            class promise_moved:
+                public future_error
+            {
+            public:
+                  promise_moved():
+                  future_error(system::make_error_code(future_errc::no_state))
+                {}
+            };
 
     namespace future_state
     {
@@ -171,20 +171,10 @@
     namespace detail
     {
 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
-        struct future_continuation_base
-        {
-          future_continuation_base() {}
-          virtual ~future_continuation_base() {}
-
-          virtual void launch_continuation(boost::unique_lock<boost::mutex>& ) {};
-        private:
-          future_continuation_base(future_continuation_base const&);
-          future_continuation_base& operator=(future_continuation_base const&);
-        };
-
-        template <typename F, typename R, typename C>
-        struct future_async_continuation;
-
+      template<typename F, typename Rp, typename Fp>
+      struct future_deferred_continuation;
+      template<typename F, typename Rp, typename Fp>
+      struct future_async_continuation;
 #endif
 
         struct relocker
@@ -231,13 +221,15 @@
             bool thread_was_interrupted;
 //#endif
 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
-            shared_ptr<future_continuation_base> continuation_ptr;
+            typedef shared_ptr<future_object_base> continuation_ptr_type;
 #else
-            shared_ptr<void> continuation_ptr;
+            typedef shared_ptr<void> continuation_ptr_type;
 #endif
+            continuation_ptr_type continuation_ptr;
+
 
 //#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
-            virtual void launch_continuation(boost::unique_lock<boost::mutex>& )
+            virtual void launch_continuation(boost::unique_lock<boost::mutex>&)
             {
             }
 //#endif
@@ -246,9 +238,12 @@
                 is_deferred_(false),
                 policy_(launch::none),
                 is_constructed(false)
+
+            // This declaration should be only included conditinally, but are included to maintain the same layout.
 //#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
               , thread_was_interrupted(false)
 //#endif
+            // This declaration should be only included conditinally, but are included to maintain the same layout.
 //#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
                , continuation_ptr()
 //#endif
@@ -289,6 +284,9 @@
             {
                 if (continuation_ptr) {
                   continuation_ptr->launch_continuation(lock);
+                  if (! lock.owns_lock())
+                    lock.lock();
+                  continuation_ptr.reset();
                 }
             }
 #else
@@ -297,9 +295,9 @@
             }
 #endif
 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
-            void set_continuation_ptr(future_continuation_base* continuation, boost::unique_lock<boost::mutex>& lock)
+            void set_continuation_ptr(continuation_ptr_type continuation, boost::unique_lock<boost::mutex>& lock)
             {
-              continuation_ptr.reset(continuation);
+              continuation_ptr= continuation;
               if (done) {
                 do_continuation(lock);
               }
@@ -336,7 +334,7 @@
             void wait_internal(boost::unique_lock<boost::mutex> &lock, bool rethrow=true)
             {
               do_callback(lock);
-              //if (!done) // fixme why this doesn't works?
+              //if (!done) // fixme why this doesn't work?
               {
                 if (is_deferred_)
                 {
@@ -506,26 +504,27 @@
         template<typename T>
         struct future_traits
         {
-            typedef boost::scoped_ptr<T> storage_type;
-            struct dummy;
+          typedef boost::scoped_ptr<T> storage_type;
+          struct dummy;
 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
-            typedef T const& source_reference_type;
-            //typedef typename boost::mpl::if_<boost::is_fundamental<T>,dummy&,BOOST_THREAD_RV_REF(T)>::type rvalue_source_type;
-            typedef BOOST_THREAD_RV_REF(T) rvalue_source_type;
-            //typedef typename boost::mpl::if_<boost::is_fundamental<T>,T,BOOST_THREAD_RV_REF(T)>::type move_dest_type;
-            typedef T move_dest_type;
+          typedef T const& source_reference_type;
+          //typedef typename boost::mpl::if_<boost::is_fundamental<T>,dummy&,BOOST_THREAD_RV_REF(T)>::type rvalue_source_type;
+          typedef BOOST_THREAD_RV_REF(T) rvalue_source_type;
+          //typedef typename boost::mpl::if_<boost::is_fundamental<T>,T,BOOST_THREAD_RV_REF(T)>::type move_dest_type;
+          typedef T move_dest_type;
 #elif defined BOOST_THREAD_USES_MOVE
-            typedef typename boost::mpl::if_c<boost::is_fundamental<T>::value,T,T&>::type source_reference_type;
-            //typedef typename boost::mpl::if_c<boost::is_fundamental<T>::value,T,BOOST_THREAD_RV_REF(T)>::type rvalue_source_type;
-            //typedef typename boost::mpl::if_c<boost::enable_move_utility_emulation<T>::value,BOOST_THREAD_RV_REF(T),T>::type move_dest_type;
-            typedef BOOST_THREAD_RV_REF(T) rvalue_source_type;
-            typedef T move_dest_type;
-#else
-            typedef T& source_reference_type;
-            typedef typename boost::mpl::if_<boost::thread_detail::is_convertible<T&,BOOST_THREAD_RV_REF(T) >,BOOST_THREAD_RV_REF(T),T const&>::type rvalue_source_type;
-            typedef typename boost::mpl::if_<boost::thread_detail::is_convertible<T&,BOOST_THREAD_RV_REF(T) >,BOOST_THREAD_RV_REF(T),T>::type move_dest_type;
+          typedef typename boost::mpl::if_c<boost::is_fundamental<T>::value,T,T&>::type source_reference_type;
+          //typedef typename boost::mpl::if_c<boost::is_fundamental<T>::value,T,BOOST_THREAD_RV_REF(T)>::type rvalue_source_type;
+          //typedef typename boost::mpl::if_c<boost::enable_move_utility_emulation<T>::value,BOOST_THREAD_RV_REF(T),T>::type move_dest_type;
+          typedef BOOST_THREAD_RV_REF(T) rvalue_source_type;
+          typedef T move_dest_type;
+#else
+          typedef T& source_reference_type;
+          typedef typename boost::mpl::if_<boost::thread_detail::is_convertible<T&,BOOST_THREAD_RV_REF(T) >,BOOST_THREAD_RV_REF(T),T const&>::type rvalue_source_type;
+          typedef typename boost::mpl::if_<boost::thread_detail::is_convertible<T&,BOOST_THREAD_RV_REF(T) >,BOOST_THREAD_RV_REF(T),T>::type move_dest_type;
 #endif
 
+
             typedef const T& shared_future_get_result_type;
 
             static void init(storage_type& storage,source_reference_type t)
@@ -553,8 +552,8 @@
         {
             typedef T* storage_type;
             typedef T& source_reference_type;
-            struct rvalue_source_type
-            {};
+            //struct rvalue_source_type
+            //{};
             typedef T& move_dest_type;
             typedef T& shared_future_get_result_type;
 
@@ -606,8 +605,7 @@
             {}
 
             ~future_object()
-            {
-            }
+            {}
 
             void mark_finished_with_result_internal(source_reference_type result_, boost::unique_lock<boost::mutex>& lock)
             {
@@ -634,12 +632,12 @@
             void mark_finished_with_result(rvalue_source_type result_)
             {
                 boost::unique_lock<boost::mutex> lock(mutex);
+
 #if ! defined  BOOST_NO_CXX11_RVALUE_REFERENCES
                 mark_finished_with_result_internal(boost::forward<T>(result_), lock);
 #else
                 mark_finished_with_result_internal(static_cast<rvalue_source_type>(result_), lock);
 #endif
-
             }
 
 
@@ -670,6 +668,7 @@
             }
 
 
+            //void set_value_at_thread_exit(const T & result_)
             void set_value_at_thread_exit(source_reference_type result_)
             {
               unique_lock<boost::mutex> lk(this->mutex);
@@ -683,6 +682,7 @@
               this->is_constructed = true;
               get_current_thread_data()->make_ready_at_thread_exit(shared_from_this());
             }
+            //void set_value_at_thread_exit(BOOST_THREAD_RV_REF(T) result_)
             void set_value_at_thread_exit(rvalue_source_type result_)
             {
               unique_lock<boost::mutex> lk(this->mutex);
@@ -706,7 +706,7 @@
         {
             typedef typename future_traits<T&>::storage_type storage_type;
             typedef typename future_traits<T&>::source_reference_type source_reference_type;
-            typedef typename future_traits<T&>::rvalue_source_type rvalue_source_type;
+            //typedef typename future_traits<T&>::rvalue_source_type rvalue_source_type;
             typedef typename future_traits<T&>::move_dest_type move_dest_type;
             typedef typename future_traits<T&>::shared_future_get_result_type shared_future_get_result_type;
 
@@ -876,14 +876,12 @@
 
           ~future_async_object()
           {
-            if (thr_.joinable()) {
-              thr_.join();
-            }
+            if (thr_.joinable()) thr_.join();
           }
 
           move_dest_type get()
           {
-              //if (thr_.joinable()) thr_.join();
+              if (thr_.joinable()) thr_.join();
               // fixme Is the lock needed during the whole scope?
               //this->wait();
               boost::unique_lock<boost::mutex> lock(this->mutex);
@@ -892,9 +890,7 @@
               //return static_cast<move_dest_type>(*(this->result));
               //return boost::move<Rp>(*(this->result));
               return boost::move(*(this->result));
-
           }
-
           static void run(future_async_object* that, BOOST_THREAD_FWD_REF(Fp) f)
           {
             try
@@ -918,12 +914,11 @@
         struct future_async_object<void, Fp>: public future_object<void>
         {
           typedef future_object<void> base_type;
-
           boost::thread thr_;
 
         public:
           explicit future_async_object(BOOST_THREAD_FWD_REF(Fp) f) :
-           thr_(&future_async_object::run, this, boost::forward<Fp>(f))
+            thr_(&future_async_object::run, this, boost::forward<Fp>(f))
           {
             this->set_async();
           }
@@ -975,7 +970,7 @@
 
           move_dest_type get()
           {
-              //if (thr_.joinable()) thr_.join();
+              if (thr_.joinable()) thr_.join();
               // fixme Is the lock needed during the whole scope?
               //this->wait();
               boost::unique_lock<boost::mutex> lock(this->mutex);
@@ -983,7 +978,6 @@
 
               //return static_cast<move_dest_type>(*(this->result));
               return boost::move(*(this->result));
-
           }
           static void run(future_async_object* that, BOOST_THREAD_FWD_REF(Fp) f)
           {
@@ -1031,7 +1025,6 @@
             }
           }
         };
-
         template<typename Rp, typename Fp>
         struct future_deferred_object<Rp&,Fp>: future_object<Rp&>
         {
@@ -1470,6 +1463,17 @@
         BOOST_THREAD_FUTURE<Rp>
         make_future_deferred_object(BOOST_THREAD_FWD_REF(Fp) f);
 #endif // #if (!defined _MSC_VER || _MSC_VER >= 1400)
+
+#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
+        template <class F, class Rp, class Fp>
+        BOOST_THREAD_FUTURE<Rp>
+        make_future_async_continuation(boost::unique_lock<boost::mutex> &lock, F& f, BOOST_THREAD_FWD_REF(Fp) c);
+
+        template <class F, class Rp, class Fp>
+        BOOST_THREAD_FUTURE<Rp>
+        make_future_deferred_continuation(boost::unique_lock<boost::mutex> &lock, F& f, BOOST_THREAD_FWD_REF(Fp) c);
+#endif
+
     }
 
     template <typename R>
@@ -1484,6 +1488,8 @@
 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
         template <typename, typename, typename>
         friend struct detail::future_async_continuation;
+        template <typename, typename, typename>
+        friend struct detail::future_deferred_continuation;
 #endif
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
         template <class> friend class packaged_task; // todo check if this works in windows
@@ -1500,6 +1506,16 @@
         friend BOOST_THREAD_FUTURE<Rp>
         detail::make_future_deferred_object(BOOST_THREAD_FWD_REF(Fp) f);
 
+#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
+        template <class F, class Rp, class Fp>
+        friend BOOST_THREAD_FUTURE<Rp>
+        detail::make_future_async_continuation(boost::unique_lock<boost::mutex> &lock, F& f, BOOST_THREAD_FWD_REF(Fp) c);
+
+        template <class F, class Rp, class Fp>
+        friend BOOST_THREAD_FUTURE<Rp>
+        detail::make_future_deferred_continuation(boost::unique_lock<boost::mutex> &lock, F& f, BOOST_THREAD_FWD_REF(Fp) c);
+#endif
+
         typedef typename detail::future_traits<R>::move_dest_type move_dest_type;
 
         BOOST_THREAD_FUTURE(future_ptr a_future):
@@ -1536,6 +1552,17 @@
             static_cast<base_type*>(this)->swap(other);
         }
 
+        // todo this function must be private and friendship provided to the internal users.
+        void set_async()
+        {
+          this->future_->set_async();
+        }
+        // todo this function must be private and friendship provided to the internal users.
+        void set_deferred()
+        {
+          this->future_->set_deferred();
+        }
+
         // retrieving the value
         move_dest_type get()
         {
@@ -1547,6 +1574,7 @@
             future_ptr fut_=this->future_;
             this->future_.reset();
             return fut_->get();
+
 #else
             return this->future_->get();
 #endif
@@ -1558,18 +1586,18 @@
 //        template<typename F>
 //        auto then(F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
 
-#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
-        template<typename RF>
-        inline BOOST_THREAD_FUTURE<RF> then(RF(*func)(BOOST_THREAD_FUTURE&));
-        template<typename RF>
-        inline BOOST_THREAD_FUTURE<RF> then(launch policy, RF(*func)(BOOST_THREAD_FUTURE&));
-#endif
+//#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
+//        template<typename RF>
+//        inline BOOST_THREAD_FUTURE<RF> then(RF(*func)(BOOST_THREAD_FUTURE&));
+//        template<typename RF>
+//        inline BOOST_THREAD_FUTURE<RF> then(launch policy, RF(*func)(BOOST_THREAD_FUTURE&));
+//#endif
         template<typename F>
         inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE&)>::type>
-        then(BOOST_THREAD_RV_REF(F) func);
+        then(BOOST_THREAD_FWD_REF(F) func);
         template<typename F>
         inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE&)>::type>
-        then(launch policy, BOOST_THREAD_RV_REF(F) func);
+        then(launch policy, BOOST_THREAD_FWD_REF(F) func);
 #endif
     };
 
@@ -1758,11 +1786,7 @@
                 boost::throw_exception(future_already_retrieved());
             }
             future_obtained=true;
-            return
-                //BOOST_THREAD_MAKE_RV_REF(
-                BOOST_THREAD_FUTURE<R>(future_)
-                //)
-                ;
+            return BOOST_THREAD_FUTURE<R>(future_);
         }
 
         void set_value(typename detail::future_traits<R>::source_reference_type r)
@@ -1930,7 +1954,6 @@
             }
             future_obtained=true;
             return BOOST_THREAD_FUTURE<R&>(future_);
-            //return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<R&>(future_));
         }
 
         void set_value(R& r)
@@ -2076,9 +2099,6 @@
             }
             future_obtained=true;
             return BOOST_THREAD_FUTURE<void>(future_);
-            //return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<void>(future_));
-            //BOOST_THREAD_FUTURE<void> res;
-            //return boost::move(res);
         }
 
         void set_value()
@@ -2194,7 +2214,6 @@
 #endif
             }
 
-
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
             virtual void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)=0;
             void apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
@@ -2310,8 +2329,7 @@
 #else
                   this->mark_finished_with_result(f());
 #endif
-
-                }
+                  }
 #endif
 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
                 catch(thread_interrupted& )
@@ -2415,7 +2433,6 @@
             }
         };
 
-
 #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
 
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
@@ -2455,8 +2472,8 @@
                 {
                     try
                     {
-                      R r((f()));
-                      this->set_value_at_thread_exit(boost::move(r));
+                        R r((f()));
+                        this->set_value_at_thread_exit(boost::move(r));
                     }
 #endif
 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
@@ -2581,7 +2598,6 @@
                 }
             };
 #endif
-
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
         template<typename F, typename ...ArgTypes>
@@ -3026,7 +3042,6 @@
             {
                 future_obtained=true;
                 return BOOST_THREAD_FUTURE<R>(task);
-                //return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<R>(task));
             }
             else
             {
@@ -3165,6 +3180,7 @@
               packaged_task_type pt( f );
 
               BOOST_THREAD_FUTURE<R> ret = BOOST_THREAD_MAKE_RV_REF(pt.get_future());
+              ret.set_async();
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
               boost::thread( boost::move(pt),  boost::forward<ArgTypes>(args)... ).detach();
 #else
@@ -3177,13 +3193,21 @@
               packaged_task_type pt( f );
 
               BOOST_THREAD_FUTURE<R> ret = pt.get_future();
+              ret.set_deferred();
               return ::boost::move(ret);
             } else {
+              //BOOST_THREAD_LOG << "ERROR async "<< int(policy) << BOOST_THREAD_END_LOG;
               BOOST_THREAD_FUTURE<R> ret;
               return ::boost::move(ret);
             }
         }
 
+//        template <class R>
+//        BOOST_THREAD_FUTURE<R>
+//        async(R(*f)())
+//        {
+//            return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f));
+//        }
 #endif
 
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
@@ -3236,6 +3260,7 @@
           packaged_task_type pt( boost::forward<F>(f) );
 
           BOOST_THREAD_FUTURE<R> ret = pt.get_future();
+          ret.set_async();
 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
           boost::thread( boost::move(pt), boost::forward<ArgTypes>(args)... ).detach(); // todo forward
 #else
@@ -3264,51 +3289,52 @@
 #endif
 
         } else {
+          //BOOST_THREAD_LOG << "ERROR async "<< int(policy) << BOOST_THREAD_END_LOG;
           BOOST_THREAD_FUTURE<R> ret;
           return ::boost::move(ret);
         }
     }
 
-    ////////////////////////////////
-    // template <class F, class... ArgTypes>
-    // future<R> async(F&&, ArgTypes&&...);
-    ////////////////////////////////
+        ////////////////////////////////
+        // template <class F, class... ArgTypes>
+        // future<R> async(F&&, ArgTypes&&...);
+        ////////////////////////////////
 
-#if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR
+    #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR
 
-#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
-        template <class R, class... ArgTypes>
-        BOOST_THREAD_FUTURE<R>
-        async(R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args)
-        {
-          return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f, boost::forward<ArgTypes>(args)...));
-        }
-#else
-        template <class R>
-        BOOST_THREAD_FUTURE<R>
-        async(R(*f)())
-        {
-          return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f));
-        }
-#endif
-#endif
+    #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
+            template <class R, class... ArgTypes>
+            BOOST_THREAD_FUTURE<R>
+            async(R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args)
+            {
+              return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f, boost::forward<ArgTypes>(args)...));
+            }
+    #else
+            template <class R>
+            BOOST_THREAD_FUTURE<R>
+            async(R(*f)())
+            {
+              return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f));
+            }
+    #endif
+    #endif
 
-#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
-        template <class F, class ...ArgTypes>
-        BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
-            typename decay<ArgTypes>::type...
-        )>::type>
-        async(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args)
-        {
-            return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward<F>(f), boost::forward<ArgTypes>(args)...));
-        }
-#else
-        template <class F>
-        BOOST_THREAD_FUTURE<typename boost::result_of<F()>::type>
-        async(BOOST_THREAD_RV_REF(F) f)
-        {
-            return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward<F>(f)));
-        }
+    #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
+            template <class F, class ...ArgTypes>
+            BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
+                typename decay<ArgTypes>::type...
+            )>::type>
+            async(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args)
+            {
+                return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward<F>(f), boost::forward<ArgTypes>(args)...));
+            }
+    #else
+    template <class F>
+    BOOST_THREAD_FUTURE<typename boost::result_of<F()>::type>
+    async(BOOST_THREAD_RV_REF(F) f)
+    {
+        return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward<F>(f)));
+    }
 #endif
 
 
@@ -3381,6 +3407,7 @@
       return BOOST_THREAD_MAKE_RV_REF(p.get_future());
   }
 #endif
+
   ////////////////////////////////
   // make_shared_future deprecated
   ////////////////////////////////
@@ -3431,116 +3458,352 @@
     p.set_exception(ex);
     return p.get_future().share();
   }
+
   ////////////////////////////////
   // detail::future_async_continuation
   ////////////////////////////////
 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
   namespace detail
   {
-      template <typename F, typename R, typename C>
-      struct future_async_continuation : future_continuation_base
+
+    /////////////////////////
+    /// future_async_continuation
+    /////////////////////////
+
+    template<typename F, typename Rp, typename Fp>
+    struct future_async_continuation: future_object<Rp>
+    {
+      typedef future_object<Rp> base_type;
+      typedef typename base_type::move_dest_type move_dest_type;
+      typedef weak_ptr<future_object_base> parent_ptr_type;
+
+      F parent;
+      Fp continuation;
+      boost::thread thr_;
+
+    public:
+      explicit future_async_continuation(
+          F& f, BOOST_THREAD_FWD_REF(Fp) c
+          ) :
+      parent(f.future_),
+      //continuation(boost::move(c)),
+      continuation(c),
+      thr_()
+      {
+        this->set_async();
+      }
+
+      ~future_async_continuation()
+      {
+        if (thr_.get_id()==thread::id())
+        {
+          //BOOST_THREAD_LOG << "ERRORRRRRRRRR ~future_async_continuation " << this << " " << thr_.get_id() << BOOST_THREAD_END_LOG;
+          return;
+        }
+        if (thr_.joinable()) {
+          thr_.join();
+        }
+      }
+
+      void launch_continuation(boost::unique_lock<boost::mutex>& lock)
+      {
+        lock.unlock();
+        thr_ = thread(&future_async_continuation::run, this);
+      }
+
+      move_dest_type get()
+      {
+          if (thr_.joinable()) thr_.join();
+          // fixme Is the lock needed during the whole scope?
+          //this->wait();
+          boost::unique_lock<boost::mutex> lock(this->mutex);
+          this->wait_internal(lock);
+
+          // fixme use boost::move
+          return static_cast<move_dest_type>(*(this->result));
+      }
+      static void run(future_async_continuation* that)
       {
-        F& parent;
-        C continuation;
-        launch policy_;
-        promise<R> next;
+        try
+        {
+          that->mark_finished_with_result(that->continuation(that->parent));
+        }
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
+        catch(thread_interrupted& )
+        {
+          that->mark_interrupted_finish();
+        }
+#endif
+        catch(...)
+        {
+          that->mark_exceptional_finish();
+        }
+      }
+    };
 
-        future_async_continuation(F& f, BOOST_THREAD_FWD_REF(C) c) :
+    template<typename F, typename Fp>
+    struct future_async_continuation<F, void, Fp>: public future_object<void>
+    {
+      typedef future_object<void> base_type;
+      F& parent;
+      Fp continuation;
+      boost::thread thr_;
+
+    public:
+      explicit future_async_continuation(
+          F& f, BOOST_THREAD_FWD_REF(Fp) c
+          ) :
+      parent(f),
+      continuation(boost::move(c)),
+      thr_()
+      {
+        this->set_async();
+      }
+
+      ~future_async_continuation()
+      {
+        if (thr_.get_id()==thread::id())
+        {
+          return;
+        }
+        if (thr_.joinable()) thr_.join();
+      }
+
+      void launch_continuation(boost::unique_lock<boost::mutex>& lk)
+      {
+        lk.unlock();
+        thr_ = thread(&future_async_continuation::run, this);
+      }
+
+      static void run(future_async_continuation* that)
+      {
+        try
+        {
+          that->continuation(that->parent);
+          that->mark_finished_with_result();
+        }
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
+        catch(thread_interrupted& )
+        {
+          that->mark_interrupted_finish();
+        }
+#endif
+        catch(...)
+        {
+          that->mark_exceptional_finish();
+        }
+      }
+    };
+
+
+    //////////////////////////
+    /// future_deferred_continuation
+    //////////////////////////
+    template<typename F, typename Rp, typename Fp>
+    struct future_deferred_continuation: future_object<Rp>
+    {
+      typedef future_object<Rp> base_type;
+      F& parent;
+      Fp continuation;
+
+    public:
+      explicit future_deferred_continuation(
+          F& f, BOOST_THREAD_FWD_REF(Fp) c
+          ) :
           parent(f),
-          continuation(boost::forward<C>(c)),
-          policy_(f.launch_policy()),
-          next()
-        {}
-        future_async_continuation(F& f, BOOST_THREAD_FWD_REF(C) c, launch policy) :
+          //continuation(boost::move(c))
+          continuation(c)
+      {
+        this->set_deferred();
+      }
+
+      virtual void launch_continuation(boost::unique_lock<boost::mutex>& lk)
+      {
+        execute(lk);
+      }
+
+      virtual void execute(boost::unique_lock<boost::mutex>& lck) {
+        try
+        {
+          this->mark_finished_with_result_internal(continuation(parent), lck);
+        }
+        catch (...)
+        {
+          this->mark_exceptional_finish_internal(current_exception(), lck);
+        }
+      }
+    };
+
+    template<typename F, typename Fp>
+    struct future_deferred_continuation<F,void,Fp>: future_object<void>
+    {
+      typedef future_object<void> base_type;
+      F& parent;
+      Fp continuation;
+
+    public:
+      explicit future_deferred_continuation(
+          F& f, BOOST_THREAD_FWD_REF(Fp) c
+          ):
           parent(f),
-          continuation(boost::forward<C>(c)),
-          policy_(policy),
-          next()
-        {}
-        ~future_async_continuation()
-        {}
+          continuation(boost::move(c))
+      {
+        this->set_deferred();
+      }
 
-        void launch_continuation(boost::unique_lock<boost::mutex>& lk)
+      virtual void launch_continuation(boost::unique_lock<boost::mutex>& lk)
+      {
+        execute(lk);
+      }
+      virtual void execute(boost::unique_lock<boost::mutex>& lck) {
+        try
         {
-          try
-          {
-            lk.unlock();
-            // fixme what to do depending on inherits_launch_policy_ and policy_?
+          continuation(parent);
+          this->mark_finished_with_result_internal(lck);
+        }
+        catch (...)
+        {
+          this->mark_exceptional_finish_internal(current_exception(), lck);
+        }
+      }
+    };
+
+    ////////////////////////////////
+    // make_future_deferred_continuation
+    ////////////////////////////////
+    template<typename F, typename Rp, typename Fp>
+    BOOST_THREAD_FUTURE<Rp>
+    make_future_deferred_continuation(
+        boost::unique_lock<boost::mutex> &lock,
+        F& f, BOOST_THREAD_FWD_REF(Fp) c
+        )
+    {
+      shared_ptr<future_deferred_continuation<F, Rp, Fp> >
+          h(new future_deferred_continuation<F, Rp, Fp>(f, boost::forward<Fp>(c)));
+      f.future_->set_continuation_ptr(h, lock);
+      return BOOST_THREAD_FUTURE<Rp>(h);
+    }
+
+    ////////////////////////////////
+    // make_future_async_continuation
+    ////////////////////////////////
+    template<typename F, typename Rp, typename Fp>
+    BOOST_THREAD_FUTURE<Rp>
+    make_future_async_continuation(
+        boost::unique_lock<boost::mutex> &lock, F& f, BOOST_THREAD_FWD_REF(Fp) c
+        )
+    {
+      shared_ptr<future_async_continuation<F,Rp, Fp> >
+          h(new future_async_continuation<F,Rp, Fp>(f, boost::forward<Fp>(c)));
+      f.future_->set_continuation_ptr(h, lock);
+
+      return BOOST_THREAD_FUTURE<Rp>(h);
+    }
+
+//      template <typename F, typename R, typename C>
+//      struct future_continuation : future_object<R>
+//      {
+//        F& parent;
+//        C continuation;
+//        launch policy_;
+//
+//        future_continuation(boost::unique_lock<boost::mutex>& lk, F& f, BOOST_THREAD_FWD_REF(C) c) :
+//          parent(f),
+//          continuation(boost::forward<C>(c)),
+//          policy_(f.launch_policy())
+//        {
+//          init_continuation(lk);
+//        }
+//        future_continuation(boost::unique_lock<boost::mutex>& lk, F& f, BOOST_THREAD_FWD_REF(C) c, launch policy) :
+//          parent(f),
+//          continuation(boost::forward<C>(c)),
+//          policy_(policy)
+//        {
+//          init_continuation(lk);
+//        }
+//        ~future_continuation()
+//        {}
+//
+//        void init_continuation(boost::unique_lock<boost::mutex>& lk)
+//        {
+//          try
+//          {
+//            lk.unlock();
+//            // fixme what to do depending on inherits_launch_policy_ and policy_?
 //            if (int(policy_) & int(launch::deferred))
-            {
-              R val = continuation(parent);
-              next.set_value(boost::move(val));
-            }
+//            {
+//              R val = continuation(parent);
+//              next.set_value(boost::move(val));
+//            }
 //            else
 //            {
 //              BOOST_THREAD_FUTURE<R> f = async(policy_, continuation, boost::ref(parent));
 //              R val = f.get();
 //              next.set_value(boost::move(val));
 //            }
-          }
-          catch (...)
-          {
-            next.set_exception(boost::current_exception());
-          }
-        }
-      private:
-
-        future_async_continuation(future_async_continuation const&);
-        future_async_continuation& operator=(future_async_continuation const&);
-      };
-#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
-      template <typename F, typename R, typename CR>
-      struct future_async_continuation<F,R,CR(*)(F&)> : future_continuation_base
-      {
-        F& parent;
-        CR(*continuation)(F&) ;
-        launch policy_;
-        promise<R> next;
-
-        future_async_continuation(F& f, CR(*c)(F&)) :
-          parent(f),
-          continuation(c),
-          policy_(f.launch_policy()),
-          next()
-        {}
-        future_async_continuation(F& f, CR(*c)(F&), launch policy) :
-          parent(f),
-          continuation(c),
-          policy_(policy),
-          next()
-        {}
-        ~future_async_continuation()
-        {}
-
-        void launch_continuation(boost::unique_lock<boost::mutex>& lk)
-        {
-          try
-          {
-            lk.unlock();
-            // fixme what to do depending on inherits_launch_policy_ and policy_?
+//          }
+//          catch (...)
+//          {
+//            next.set_exception(boost::current_exception());
+//          }
+//        }
+//      private:
+//
+//        future_continuation(future_continuation const&);
+//        future_continuation& operator=(future_continuation const&);
+//      };
+//#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
+//      template <typename F, typename R, typename CR>
+//      struct future_continuation<F,R,CR(*)(F&)> : future_object<R>
+//      {
+//        F& parent;
+//        CR(*continuation)(F&) ;
+//        launch policy_;
+//
+//        future_continuation(F& f, CR(*c)(F&)) :
+//          parent(f),
+//          continuation(c),
+//          policy_(f.launch_policy()),
+//          next()
+//        {}
+//        future_continuation(F& f, CR(*c)(F&), launch policy) :
+//          parent(f),
+//          continuation(c),
+//          policy_(policy),
+//          next()
+//        {}
+//        ~future_continuation()
+//        {}
+//
+//        void start_continuation(boost::unique_lock<boost::mutex>& lk)
+//        {
+//          try
+//          {
+//            lk.unlock();
+//            // fixme what to do depending on inherits_launch_policy_ and policy_?
 //            if (int(policy_) & int(launch::deferred))
-            {
-              R val = continuation(parent);
-              next.set_value(boost::move(val));
-            }
+//            {
+//              R val = continuation(parent);
+//              next.set_value(boost::move(val));
+//            }
 //            else
 //            {
 //              BOOST_THREAD_FUTURE<R> f = async(policy_, continuation, boost::ref(parent));
 //              R val = f.get();
 //              next.set_value(boost::move(val));
 //            }
-          }
-          catch (...)
-          {
-            next.set_exception(boost::current_exception());
-          }
-        }
-      private:
-
-        future_async_continuation(future_async_continuation const&);
-        future_async_continuation& operator=(future_async_continuation const&);
-      };
-#endif
+//          }
+//          catch (...)
+//          {
+//            next.set_exception(boost::current_exception());
+//          }
+//        }
+//      private:
+//
+//        future_continuation(future_continuation const&);
+//        future_continuation& operator=(future_continuation const&);
+//      };
+//#endif
   }
 
   ////////////////////////////////
@@ -3551,119 +3814,125 @@
   template <typename R>
   template <typename F>
   inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<R>&)>::type>
-  BOOST_THREAD_FUTURE<R>::then(launch policy, BOOST_THREAD_RV_REF(F) func)
+  BOOST_THREAD_FUTURE<R>::then(launch policy, BOOST_THREAD_FWD_REF(F) func)
   {
 
-    typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>&)>::type future_type;
+      typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>&)>::type future_type;
 
-    if (this->future_)
-    {
-      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
-      detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, F > *ptr =
-          new detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, F>(*this, boost::forward<F>(func), policy);
-      if (ptr==0)
+      if (this->future_==0)
       {
-        return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+        // fixme what to do when the future has no associated state?
+        return BOOST_THREAD_FUTURE<future_type>();
       }
-      this->future_->set_continuation_ptr(ptr, lock);
-      return BOOST_THREAD_MAKE_RV_REF(ptr->next.get_future());
-    }
-    else
+
+      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
+      if (int(policy) & int(launch::async))
     {
-      // fixme what to do when the future has no associated state?
-      return BOOST_THREAD_FUTURE<future_type>();
+      return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, F>(
+          lock, *this, boost::forward<F>(func)
+      )));
     }
+      else
+        if (int(policy) & int(launch::deferred))
+        {
+          return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation<BOOST_THREAD_FUTURE<R>, future_type, F>(
+              lock, *this, boost::forward<F>(func)
+          )));
+        }
+      else
+      {
+        // fixme what to do when the policy is invalid?
+        return BOOST_THREAD_FUTURE<future_type>();
+      }
 
   }
   template <typename R>
   template <typename F>
   inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<R>&)>::type>
-  BOOST_THREAD_FUTURE<R>::then(BOOST_THREAD_RV_REF(F) func)
+  BOOST_THREAD_FUTURE<R>::then(BOOST_THREAD_FWD_REF(F) func)
   {
 
     typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>&)>::type future_type;
 
-    if (this->future_)
+    if (this->future_==0)
     {
-      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
-      detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, F > *ptr =
-          new
-          //BOOST_THREAD_MAKE_RV_REF((
-              detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, F>(*this, boost::forward<F>(func))
-              //))
-              ;
-      if (ptr==0)
-      {
-        return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
-      }
-      this->future_->set_continuation_ptr(ptr, lock);
-      return ptr->next.get_future();
-    } else {
+      //BOOST_THREAD_LOG << "ERROR future::then " << this << BOOST_THREAD_END_LOG;
       // fixme what to do when the future has no associated state?
-      return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+      return BOOST_THREAD_FUTURE<future_type>();
     }
 
-  }
-#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
-  template <typename R>
-  template<typename RF>
-  BOOST_THREAD_FUTURE<RF>
-  BOOST_THREAD_FUTURE<R>::then(RF(*func)(BOOST_THREAD_FUTURE<R>&))
-  {
-
-    typedef RF future_type;
-
-    if (this->future_)
+    boost::unique_lock<boost::mutex> lock(this->future_->mutex);
+    if (int(this->launch_policy()) & int(launch::async))
     {
-      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
-      detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&) > *ptr =
-          new
-          //BOOST_THREAD_MAKE_RV_REF((
-              detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&)>(*this, func)
-           //   ))
-      ;
-      if (ptr==0)
-      {
-        return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
-      }
-      this->future_->set_continuation_ptr(ptr, lock);
-      return ptr->next.get_future();
-    } else {
-      // fixme what to do when the future has no associated state?
-      return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+      return boost::detail::make_future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, F>(
+          lock, *this, boost::forward<F>(func)
+      );
+    }
+    else if (int(this->launch_policy()) & int(launch::deferred))
+    {
+      this->future_->wait_internal(lock);
+      return boost::detail::make_future_deferred_continuation<BOOST_THREAD_FUTURE<R>, future_type, F>(
+          lock, *this, boost::forward<F>(func)
+      );
     }
-
-  }
-  template <typename R>
-  template<typename RF>
-  BOOST_THREAD_FUTURE<RF>
-  BOOST_THREAD_FUTURE<R>::then(launch policy, RF(*func)(BOOST_THREAD_FUTURE<R>&))
-  {
-
-    typedef RF future_type;
-
-    if (this->future_)
+    else
     {
-      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
-      detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&) > *ptr =
-          new
-          //BOOST_THREAD_MAKE_RV_REF((
-              detail::future_async_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&)>(*this, func, policy)
-            //  ))
-      ;
-      if (ptr==0)
-      {
-        return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
-      }
-      this->future_->set_continuation_ptr(ptr, lock);
-      return ptr->next.get_future();
-    } else {
-      // fixme what to do when the future has no associated state?
-      return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+      // fixme what to do when the policy is invalid?
+      return BOOST_THREAD_FUTURE<future_type>();
     }
-
   }
-#endif
+//#if 0 && defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR)
+//  template <typename R>
+//  template<typename RF>
+//  BOOST_THREAD_FUTURE<RF>
+//  BOOST_THREAD_FUTURE<R>::then(RF(*func)(BOOST_THREAD_FUTURE<R>&))
+//  {
+//
+//    typedef RF future_type;
+//
+//    if (this->future_)
+//    {
+//      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
+//      detail::future_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&) > *ptr =
+//          new detail::future_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&)>(*this, func);
+//      if (ptr==0)
+//      {
+//        return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+//      }
+//      this->future_->set_continuation_ptr(ptr, lock);
+//      return ptr->get_future();
+//    } else {
+//      // fixme what to do when the future has no associated state?
+//      return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+//    }
+//
+//  }
+//  template <typename R>
+//  template<typename RF>
+//  BOOST_THREAD_FUTURE<RF>
+//  BOOST_THREAD_FUTURE<R>::then(launch policy, RF(*func)(BOOST_THREAD_FUTURE<R>&))
+//  {
+//
+//    typedef RF future_type;
+//
+//    if (this->future_)
+//    {
+//      boost::unique_lock<boost::mutex> lock(this->future_->mutex);
+//      detail::future_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&) > *ptr =
+//          new detail::future_continuation<BOOST_THREAD_FUTURE<R>, future_type, RF(*)(BOOST_THREAD_FUTURE&)>(*this, func, policy);
+//      if (ptr==0)
+//      {
+//        return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+//      }
+//      this->future_->set_continuation_ptr(ptr, lock);
+//      return ptr->get_future();
+//    } else {
+//      // fixme what to do when the future has no associated state?
+//      return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<future_type>());
+//    }
+//
+//  }
+//#endif
 
 #endif
 
Modified: trunk/libs/thread/example/future_then.cpp
==============================================================================
--- trunk/libs/thread/example/future_then.cpp	(original)
+++ trunk/libs/thread/example/future_then.cpp	2013-04-01 12:17:05 EDT (Mon, 01 Apr 2013)
@@ -3,7 +3,7 @@
 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
-//#define BOOST_THREAD_VERSION 4
+#define BOOST_THREAD_VERSION 4
 #define BOOST_THREAD_USES_LOG
 #define BOOST_THREAD_USES_LOG_THREAD_ID
 #define BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET
@@ -46,11 +46,8 @@
   try
   {
     boost::future<int> f1 = boost::async(boost::launch::async, &p1);
-    BOOST_THREAD_LOG <<  BOOST_THREAD_END_LOG;
     boost::future<int> f2 = f1.then(&p2);
-    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
     int i = f2.get();
-    BOOST_THREAD_LOG << i << BOOST_THREAD_END_LOG;
   }
   catch (std::exception& ex)
   {
Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2	(original)
+++ trunk/libs/thread/test/Jamfile.v2	2013-04-01 12:17:05 EDT (Mon, 01 Apr 2013)
@@ -337,7 +337,7 @@
           [ thread-run2-noit ./sync/futures/future/wait_pass.cpp : future__wait_p ]
           [ thread-run2-noit ./sync/futures/future/wait_for_pass.cpp : future__wait_for_p ]
           [ thread-run2-noit ./sync/futures/future/wait_until_pass.cpp : future__wait_until_p ]
-          #[ thread-run2-noit ./sync/futures/future/then_pass.cpp : future__then_p ]
+          [ thread-run2-noit ./sync/futures/future/then_pass.cpp : future__then_p ]
     ;
 
     #explicit ts_shared_future ;
@@ -666,7 +666,7 @@
           #[ thread-run ../example/vhh_shared_monitor.cpp ]
           #[ thread-run ../example/vhh_shared_mutex.cpp ]
           [ thread-run ../example/make_future.cpp ]
-          #[ thread-run ../example/future_then.cpp ]
+          [ thread-run ../example/future_then.cpp ]
           [ thread-run2-noit ../example/synchronized_value.cpp : ex_synchronized_value ]
           [ thread-run2-noit ../example/synchronized_person.cpp : ex_synchronized_person ]
           [ thread-run2-noit ../example/thread_guard.cpp : ex_thread_guard ]
Modified: trunk/libs/thread/test/sync/futures/future/then_pass.cpp
==============================================================================
--- trunk/libs/thread/test/sync/futures/future/then_pass.cpp	(original)
+++ trunk/libs/thread/test/sync/futures/future/then_pass.cpp	2013-04-01 12:17:05 EDT (Mon, 01 Apr 2013)
@@ -12,6 +12,7 @@
 
 #define BOOST_THREAD_VERSION 4
 #define BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET
+//#define BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
 #define BOOST_THREAD_USES_LOG
 #define BOOST_THREAD_USES_LOG_THREAD_ID
 #include <boost/thread/detail/log.hpp>
@@ -23,61 +24,68 @@
 
 int p1()
 {
+  BOOST_THREAD_LOG << "p1 < " << BOOST_THREAD_END_LOG;
+  boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
+  BOOST_THREAD_LOG << "p1 >"  << BOOST_THREAD_END_LOG;
   return 1;
 }
 
 int p2(boost::future<int>& f)
 {
-  return 2 * f.get();
+  BOOST_THREAD_LOG << "p2 <" << &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 << "p2 <" << &f << BOOST_THREAD_END_LOG;
+  return 2 * i;
 }
 
 int main()
 {
+  {
+    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
+    boost::future<int> f1 = boost::async(boost::launch::async, &p1);
+    BOOST_TEST(f1.valid());
+    boost::future<int> f2 = f1.then(&p2);
+    BOOST_TEST(f2.valid());
+    try
+    {
+      BOOST_TEST(f2.get()==2);
+    }
+    catch (std::exception& ex)
+    {
+      BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
+      BOOST_TEST(false);
+    }
+    catch (...)
+    {
+      BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
+      BOOST_TEST(false);
+    }
+  }
   BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-  boost::future<int> f1 = boost::async(boost::launch::async, p1);
-  BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-  boost::future<int> f2 = f1.then(p2);
-  BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-  try
   {
+    boost::future<int> f2 = boost::async(p1).then(&p2);
     BOOST_TEST(f2.get()==2);
-    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
   }
-  catch (std::exception& ex)
+  BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
   {
-    BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
-    BOOST_ASSERT(false);
+    boost::future<int> f1 = boost::async(p1);
+    boost::future<int> f21 = f1.then(&p2);
+    boost::future<int> f2= f21.then(&p2);
+    BOOST_TEST(f2.get()==4);
   }
-  catch (...)
+  BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
   {
-    BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
-    BOOST_ASSERT(false);
+    boost::future<int> f1 = boost::async(p1);
+    boost::future<int> f2= f1.then(&p2).then(&p2);
+    BOOST_TEST(f2.get()==4);
   }
   BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-
-//  {
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    boost::future<int> f2 = boost::async(p1).then(p2);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    BOOST_TEST(f2.get()==2);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//  }
-//  {
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    boost::future<int> f1 = boost::async(p1);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    boost::future<int> f2 = f1.then(p2).then(p2);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    BOOST_TEST(f2.get()==4);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//  }
-//  {
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    boost::future<int> f2 = boost::async(p1).then(p2).then(p2);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//    BOOST_TEST(f2.get()==4);
-//    BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
-//  }
+  {
+    boost::future<int> f2 = boost::async(p1).then(&p2).then(&p2);
+    BOOST_TEST(f2.get()==4);
+  }
 
   return boost::report_errors();
 }