Boost future continuation never changes state

2019-05-21 03:28发布

i have a question concerning the boost future continuations. Consider the following code:

#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#define BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>


boost::future<int> test() {
  return boost::async([]() {
     for(int i = 0; i < 10; ++i){
       boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
     } 
     return 10;});
}

int main(int argc, char *argv[])
    //boost::future<int> f1 = test();
    boost::future<int> f1 = test().then([](boost::future<int>&& f) {
        return test();});
    int result = f1.get();
    std::cout <<f1.is_ready();
    f1.wait();
    std::cout<<" "<<f1.is_ready() <<std::endl;
}

In my opinion this should print 1 1 on the commandline. Instead i get 0 0. Is this the expected behaviour of the continuation?

Commenting out line

boost::future<int> f1 = test().then([](boost::future<int>&& f)  
 {return test();});

and uncomment line

//boost::future<int> f1 = test();

yields to the expected result.

Tested on g++ 5.2.0 and Visual Studio 2010 with boost-1.57 and boost-1.59.

Thanks for the help.

0条回答
登录 后发表回答