为什么我的带有 boost::future 的 .then 的链式方法没有被调用?
Why my chained methods with boost::future's .then are not invoked?
我有以下一段代码:
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <iostream>
#include <thread>
#include <boost/thread/future.hpp>
using namespace boost;
int foo(boost::future<int> x) {
std::cout << "first stage(" << x.get() << ")" << 'n';
return x.get();
}
int main()
{
std::cout << "making promise" << 'n';
boost::promise<int> p;
boost::future<int> f = p.get_future();
std::cout << "future chain made" << 'n';
std::thread t([&](){
f.then(foo)
.then([](boost::future<int> x){ std::cout << "second stage " << 2 * x.get() << 'n'; return 2 * x.get(); })
.then([](boost::future<int> x){ std::cout << "final stage " << 10 * x.get() << 'n'; });
});
std::cout << "fulfilling promise" << 'n';
p.set_value(42);
std::cout << "promise fulfilled" << 'n';
t.join();
}
我这样编译它:
g++ -g -Wall -std=c++14 -DBOOST_THREAD_VERSION=4 main.cpp -lboost_thread -lboost_system -pthread
我得到以下输出:
making promise
future chain made
fulfilling promise
promise fulfilled
first stage(42)
为什么我的 2 个 lambda 链接在线程中t
没有被调用?我错过了什么吗?
我尝试添加boost::future::get()
调用,但随后出现异常:
std::cout << "fulfilling promise" << 'n';
p.set_value(42);
std::cout << "promise fulfilled" << 'n';
std::cout << "value " << f.get() << 'n';
t.join();
错误:
making promise
future chain made
fulfilling promise
promise fulfilled
first stage(42)
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::future_uninitialized> >'
what(): Operation not permitted on an object without an associated state.
[1] 20875 abort ./main
我正在使用提升 1.58.0 和 gcc 5.4.0
在线源链接(带有booost 1.58.0和gcc 5.3.0)http://melpon.org/wandbox/permlink/G8rqt2eHUwI4nzz8
正如一位伟大的诗人曾经写过的那样,"等待它"。
std::thread t([&](){
f.then(foo)
.then([](boost::shared_future<int> x){ std::cout << "second stage " << 2 * x.get() << 'n'; return 2 * x.get(); })
.then([](boost::shared_future<int> x){ std::cout << "final stage " << 10 * x.get() << 'n'; })
.get();
});
该线程除了设置期货链外什么都不做。 它不运行其中任何一个。
你启动链(用你的集合),你等待链被设置(有连接),但在链完成之前主退出。 你会很"幸运",在进程退出之前运行一个。
真的,你应该在主线程中设置链,并在线程t中等待链的最后一个未来。 那么你的代码就更有意义了。
auto last = f.then(foo)
.then([](boost::shared_future<int> x){ std::cout << "second stage " << 2 * x.get() << 'n'; return 2 * x.get(); })
.then([](boost::shared_future<int> x){ std::cout << "final stage " << 10 * x.get() << 'n'; });
std::thread t([&](){
last.get();
});
这突出了线程 t 没有用处的事实:将主线程中的 t.join()
替换为 last.get()
并完全删除变量t
。
正如下面的评论中所述,您还调用了两次get:要使其正常工作,您需要一个shared_future。 这可能就是为什么你的运气始终如一的原因,因为第二个得到可能会阻塞线程。
相关文章:
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用