[Boost-bugs] [Boost C++ Libraries] #12293: in boost 1.60.0 boost::future::then lambda called before future is ready.

Subject: [Boost-bugs] [Boost C++ Libraries] #12293: in boost 1.60.0 boost::future::then lambda called before future is ready.
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-06-22 15:49:47


#12293: in boost 1.60.0 boost::future::then lambda called before future is ready.
---------------------------------+----------------------
 Reporter: Jeffrey.Gramowski@… | Owner: anthonyw
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: thread
  Version: Boost 1.60.0 | Severity: Problem
 Keywords: future then |
---------------------------------+----------------------
 When chaining .then together with anther one and the previous .then
 returns a boost::future. The lambda will get called before the future is
 ready. I think what is happening is that the lambda is being called when
 the outer hidden future is ready. I think that the callback shouldn't be
 called until the returned future is ready.

 Here is the sample code which demonstrates the problem.

 {{{
 #!C++
 // BoostFutureTest.cpp : Defines the entry point for the console
 application.
 //

 #include "stdafx.h"

 #include <future>
 // boost version 1.60.0
 // has the following set.
 // #define BOOST_THREAD_VERSION 4
 // #define BOOST_THREAD_PROVIDES_EXECUTORS

 #include <boost\thread\future.hpp>


 int _tmain(int argc, _TCHAR* argv[])
 {
     int value = 0;
     int tmpValue = 0;
     boost::promise<void> promise1;
     boost::promise<void> promise2;

     auto future1 = promise1.get_future();

     auto waitFuture = future1.then([&tmpValue,
 &promise2](boost::future<void> future){
         assert(future.is_ready()); // this works correctly and is ready.

         std::async(std::launch::async, [&promise2, &tmpValue](){
             std::this_thread::sleep_for(std::chrono::seconds(1));
             tmpValue = 1;
             promise2.set_value();
         });

         return promise2.get_future();
     }).then([&value, &tmpValue](boost::future<void> future){
         // this lambda is being called before the future is ready.
         assert(future.is_ready()); // this doesn't work correctly and is
 not ready.

         // if we call future.get() this will block until the promise2 has
 been set be I don't want
         // the then lambda to block. I want the lambda called when the
 future is ready.

         value = tmpValue;
     });


     promise1.set_value();
     waitFuture.wait();

     std::cout << "value = " << value << std::endl; // should print 1 but
 prints 0
     return 0;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12293>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC