I am using Boost’s promises and futures and encountered an edge case while using continuations. My code uses a continuation that returns a future, and unwraps then()’s result before getting its value.
#define BOOST_THREAD_VERSION 5
#include <iostream>
#include <boost/thread/future.hpp>
int main(int argc, char* argv[])
{
boost::promise<int> promise;
boost::future<int> future = promise.get_future();
promise.set_value(42);
int result = future.then(
boost::launch::async,
[](boost::future<int> result)
{
return boost::make_ready_future(result.get());
}
).unwrap().get();
std::cout << "Result is: " << result << std::endl;
return 0;
}
In this example, I explicitly used the boost::launch::async policy to launch the continuation in a new thread, and I get the expected result of 42.
However, as soon as I replace that policy with boost::launch::deferred, the program seems to deadlock. What am I doing wrong?
NB: The deferred continuation works correctly as long as I don’t unwrap() its value. The issue is specifically about unwrapped deferred continuations.
result.get()from the callback and then callget()without unwrapping?unwrap(), it actually works correctly. In this case, the use ofunwrap()is trivial but I tried to simplify the question down to a minimum. I will edit my question to make that clear, thanks for the heads-up!get()on the resulting future, but after you callunwrap()the resulting future is no longer the one you callget()on, so no callbacks are called. The callbacks essentially are not carried over. But I am not sure as to why such a decision was made by Boost, therefore the bounty might help!