使用boost :: async但不在std :: thread下调用boost :: future的延续。我正在使用" gcc版本5.3.0 20151204(Ubuntu 5.3.0-3ubuntu1~14.04)"
在以下程序中," WORKS"下的代码宏打印以下输出
承诺价值=
然后值= 3
值= 3
但其他代码路径失败。倒数第二行" newff.wait()"因以下输出而失败
承诺价值=
在抛出' boost :: exception_detail :: clone_impl>'的实例后终止调用what():没有关联状态的对象上不允许操作。中止(核心倾销)
#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;
}