我正在使用Boost的promises和futures,并在使用continuation时遇到了边缘情况.我的代码使用了一个返回未来的延续,并then()在获取其值之前解开了结果.
#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;
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我明确地使用了boost::launch::async策略在新线程中启动延续,我得到了42的预期结果.
但是,一旦我用该替换该策略boost::launch::deferred,该程序似乎就陷入僵局.我究竟做错了什么?
注意:只要我没有unwrap()它的值,延迟延续就能正常工作.问题特别是关于展开的延期延续.