使用boost ::未来"然后"延续 [英] Using boost::future with "then" continuations
问题描述
在C ++ 11 的std ::未来
的缺乏一个然后
的方法来延续附着于未来。
升压的boost ::未来
provides这一点,并有一个<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/thread/synchronization.html#thread.synchronization.futures.then\">example (我不能得到运行)
我只是无法编译
的#include&LT;&iostream的GT;
#包括LT&;串GT;
#包括LT&;升压/线程/ future.hpp&GT;提高::未来&LT; INT&GT; join2(常量标准::字符串&安培;境界){
提高::承诺&LT;&诠释GT;磷;
p.set_value(23);
返回p.get_future();
}诠释主(){
提高::未来&LT; INT&GT; F = join2(realm1); //这里,我想用f.then(..)
f.wait();
性病::法院LT&;&LT; f.get()&所述;&下;的std :: ENDL;
}
在编译
铛++ -o test5.o -c -std = C ++ 11 -stdlib = ++的libc \\
-I /家庭/ oberstet / boost_1_55_0 test5.cpp
这捞出用
test5.cpp:30:1:错误:未知类型名称'未来'
未来&LT;&诠释GT;加入(常量标准::字符串&安培;境界){
...
我感觉愚蠢;)这是怎么回事?我使用铛与3.4的libc ++和Boost 1.55(与Boost网站未修改香草源)。
将是巨大的得到一个暗示,可能也与如何使用修改的例子为例。然后(..)
来打印出结果。
解决方案(荣誉@dyp):
的#define BOOST_THREAD_PROVIDES_FUTURE
#包括LT&;升压/线程/ future.hpp&GT;
似乎是C ++ 11编译时(提供未来)进行必需的,但一个人想用但提升未来。
有关实际使用延续,另一个定义是必要的: BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
下面是一个完整的例子
的#include&LT;&iostream的GT;
#包括LT&;串GT;#定义BOOST_THREAD_PROVIDES_FUTURE
#定义BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION#包括LT&;升压/线程/ future.hpp&GT;使用名字空间boost;
诠释主(){
未来&LT;&诠释GT; F1 =异步([](){返回123;}); 未来&LT;标准::字符串&GT; F2 = f1.then([](未来&所述; INT&F)的温度{ 性病::法院LT&;&LT; f.get()&所述;&下;的std :: ENDL; //这里获得()将不会阻塞
返回的std ::字符串(sgfsdfs);
});
}
Boost.Thread有好几种版本,其中可以通过 BOOST_THREAD_VERSION
宏选择。目前,默认为 2
。
截至Boost.Thread第2版,名称的boost :: unique_future
用于该类模板(比较的boost :: shared_future
)。大概是因为的std ::未来
的规范化,较新版本的可以的使用名称的boost ::未来
。与版本开始 3
,的boost ::未来
是默认的名称。
选择哪个名称将被用于经由一个preprocessor宏完成:
在
BOOST_THREAD_VERSION == 2
定义BOOST_THREAD_PROVIDES_FUTURE
如果
要使用的boost ::未来
。当BOOST_THREAD_VERSION&GT; = 3
定义
BOOST_THREAD_DONT_PROVIDE_FUTURE
如果你想使用
的boost :: unique_future
。
块引用>从<一个href=\"http://www.boost.org/doc/libs/1_60_0/doc/html/thread/build.html#thread.build.configuration.future\"相对=nofollow>升压文档:
unique_future
VS未来
所以,你可以明确地启用
的boost ::未来
使用BOOST_THREAD_PROVIDES_FUTURE
或切换到一个更现代的版本通过设置Boost.ThreadBOOST_THREAD_VERSION
到4
,例如The C++ 11
std::future
lacks athen
method to attach continuations to the future.The Boost
boost::future
provides this, and there is an example (which I can't get running)I am simply unable to compile:
#include <iostream> #include <string> #include <boost/thread/future.hpp> boost::future<int> join2(const std::string& realm) { boost::promise<int> p; p.set_value(23); return p.get_future(); } int main () { boost::future<int> f = join2("realm1"); // here, I'd like to use f.then(..) f.wait(); std::cout << f.get() << std::endl; }
When compiling
clang++ -o test5.o -c -std=c++11 -stdlib=libc++ \ -I/home/oberstet/boost_1_55_0 test5.cpp
this bails out with
test5.cpp:30:1: error: unknown type name 'future' future<int> join(const std::string& realm) { ...
I am feeling stupid;) What is going on? I am using clang 3.4 with libc++ and Boost 1.55 (unmodified vanilla sources from Boost website).
Would be great to get a hint, probably also with an example of how to modify the example using
.then(..)
to print out the result.Solution (kudos @dyp):
#define BOOST_THREAD_PROVIDES_FUTURE #include <boost/thread/future.hpp>
seems to be required when compiling for C++ 11 (which provides future), but one wants to use Boost future nevertheless.
For actually using continuations, another define is necessary:
BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
.Here is a complete example
#include <iostream> #include <string> #define BOOST_THREAD_PROVIDES_FUTURE #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION #include <boost/thread/future.hpp> using namespace boost; int main() { future<int> f1 = async([]() { return 123; }); future<std::string> f2 = f1.then([](future<int> f) { std::cout << f.get() << std::endl; // here .get() won't block return std::string("sgfsdfs"); }); }
解决方案Boost.Thread comes in several versions of which you can choose via the
BOOST_THREAD_VERSION
macro. Currently, the default is2
.Up to version 2 of Boost.Thread, the name
boost::unique_future
was used for this class template (compare toboost::shared_future
). Probably because of the standardization ofstd::future
, more recent versions can use the nameboost::future
. Starting with version3
,boost::future
is the default name.The selection which name is to be used is done via a preprocessor macro:
When
BOOST_THREAD_VERSION==2
defineBOOST_THREAD_PROVIDES_FUTURE
if you want to useboost::future
. WhenBOOST_THREAD_VERSION>=3
defineBOOST_THREAD_DONT_PROVIDE_FUTURE
if you want to useboost::unique_future
.From boost docs:
unique_future
vsfuture
So you can either explicitly enable
boost::future
by usingBOOST_THREAD_PROVIDES_FUTURE
or switch to a more modern version of Boost.Thread by settingBOOST_THREAD_VERSION
to4
, for example.这篇关于使用boost ::未来&QUOT;然后&QUOT;延续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!