提高::未来延续 - 值而定,但未来仍有块 [英] boost::future and continuations - value set, but future still blocks
问题描述
我想作以下工作的延续 - 但 f.get()
块。什么错?
的#include<&iostream的GT;#定义BOOST_THREAD_PROVIDES_FUTURE
#定义BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#包括LT&;升压/线程/ future.hpp>结构美孚{ 提高::未来< INT>启动(){
返回p.get_future();
} 无效完成(){
p.set_value(23);
} 提高::承诺<&诠释GT;磷;
};诠释主(){ 富富; foo.start(),然后([](升压::未来< INT&F)的温度{
性病::法院LT&;< 完成:&所述;&下;的std :: ENDL;
性病::法院LT&;< f.get()&所述;&下;的std :: ENDL;
}); foo.finish();
}
这将打印完成
,所以未来的火灾,但 f.get它会再只是挂 ()
..我失去了。
要建立:
铛++ -o test8 -std = C ++ 11 -stdlib = libc中++ -lboost_thread -lboost_system \\
-I /家庭/ oberstet / boost_1_55_0 -L /家庭/ oberstet / boost_1_55_0 /台/ lib目录\\
test8.cpp
更新:下面code的改变会使这个例子的工作 - 但为什么呢?由于 F2
无论如何都不会使用。再次疑惑。
的boost ::未来<无效> F2 = foo.start(),然后([](升压::未来< INT&F)的温度{
性病::法院LT&;< 完成:&所述;&下;的std :: ENDL;
性病::法院LT&;< f.get()&所述;&下;的std :: ENDL;
});
更新2 :下面,添加启动策略 ::发射推迟
,也将工作:
foo.start(),然后(升压::推出::推迟,[](升压::未来< INT&F)的温度{
性病::法院LT&;< 完成:&所述;&下;的std :: ENDL;
性病::法院LT&;< f.get()&所述;&下;的std :: ENDL;
});
这也:
的boost ::未来< INT>启动(){
提高::未来< INT> F = p.get_future();
f.set_deferred();
返回F;
}
现在的问题是,你的未来由不在身边不停。事实上,这是一个暂时的,它得到尽快销毁的声明(以。然后()
)结束。
修正:
INT的main(){
富富; 汽车F1 = foo.start();
汽车F2 = f1.then([](升压::未来< INT&F)的温度{
性病::法院LT&;< 完成:&所述;&下;的std :: ENDL;
性病::法院LT&;< f.get()&所述;&下;的std :: ENDL;
}); foo.finish();
f2.get();
}
现在它打印
完成:
23
看它的 住在Coliru
如果您移动 f2.get()
在 foo.finish()
它将再次死锁
I am trying to make the following continuation work - but f.get()
blocks. Whats wrong?
#include <iostream>
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <boost/thread/future.hpp>
struct Foo {
boost::future<int> start() {
return p.get_future();
}
void finish() {
p.set_value(23);
}
boost::promise<int> p;
};
int main () {
Foo foo;
foo.start().then([](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
foo.finish();
}
It'll print the "done:"
, so the future fires, but it'll then just "hang" on f.get()
.. I am lost.
To build:
clang++ -o test8 -std=c++11 -stdlib=libc++ -lboost_thread -lboost_system \
-I/home/oberstet/boost_1_55_0 -L/home/oberstet/boost_1_55_0/stage/lib \
test8.cpp
UPDATE: The following code change will make the example work - but why? Since f2
isn't used anyway. Puzzled again.
boost::future<void> f2 = foo.start().then([](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
UPDATE 2: The following, adding a launch policy launch::deferred
, will also work:
foo.start().then(boost::launch::deferred, [](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
and this also:
boost::future<int> start() {
boost::future<int> f = p.get_future();
f.set_deferred();
return f;
}
The problem is, your composed future is not kept around. In fact, it is a temporary and it gets destructed as soon as the statement (with .then()
) ends.
Fix it:
int main () {
Foo foo;
auto f1 = foo.start();
auto f2 = f1.then([](boost::future<int> f) {
std::cout << "done:" << std::endl;
std::cout << f.get() << std::endl;
});
foo.finish();
f2.get();
}
Now it prints
done:
23
See it Live On Coliru
If you move the f2.get()
before the foo.finish()
it will dead lock again.
这篇关于提高::未来延续 - 值而定,但未来仍有块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!