0

The continuation for a boost::future gets called when using boost::async but not under std::thread. I am using "gcc version 5.3.0 20151204 (Ubuntu 5.3.0-3ubuntu1~14.04)"

In the following program, the code under the "WORKS" macro prints the following output

promise value=
then value=3
value=3

but the other code path fails. the second-last line "newff.wait()" fails with the following output

promise value=
terminate called after throwing an instance of 'boost::exception_detail::clone_impl>' what(): Operation not permitted on an object without an associated state. Aborted (core dumped)


#include <thread>
#include <iostream>

#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <boost/thread/future.hpp>

boost::promise<int> apromise;

int func()
{
    std::cout << "promise value=" << std::endl;
    apromise.set_value(3);
    return 3;
}

#define WORKS

int main()
{
#ifdef WORKS
    auto ff = boost::async(func);
#else // Doesn't work
    auto ff = apromise.get_future();
    std::thread t(func);
#endif

    auto newff = ff.then([&] (boost::future<int>& f)  
        {
            std::cout << "then value=" << f.get() << std::endl;
            return f.get();
        });

#ifndef WORKS
    t.join();
#endif

    newff.wait(); 
    std::cout << "value=" << newff.get() << std::endl;
}
SandeepJ
  • 341
  • 4
  • 10
  • I suspect my problem is similar to http://stackoverflow.com/questions/32046640/boost-future-continuation-never-changes-state or http://stackoverflow.com/questions/22617420/boostfuture-and-continuations-value-set-but-future-still-blocks – SandeepJ Jan 03 '16 at 10:11
  • Are you using Boost [1.54](http://melpon.org/wandbox/permlink/80zU99elDL2Ktjl7)? Your code does what you expect starting from [1.55](http://melpon.org/wandbox/permlink/m9r2tpPBqOwWsOy9) changing `[&] (boost::future& f)` to `[&] (boost::future f)`. – llonesmiz Jan 03 '16 at 10:49
  • You are right ! I upgraded to 1.55 and it worked. Wasted a few hours debugging this... – SandeepJ Jan 03 '16 at 11:09
  • If you can you should probably use 1.60, there may be further fixes/changes. – llonesmiz Jan 03 '16 at 11:11

0 Answers0