C++ boost::共享的未来和何时都具有多个连续性

C++ boost::共享的未来和何时都具有多个连续性,c++,c++11,boost,boost-thread,C++,C++11,Boost,Boost Thread,我有大量的任务要用boost::shared\u future框架来执行 例如具体性,考虑图中所示。 下面是一个尝试代码: #包括 #定义BOOST\u线程\u提供未来 #定义BOOST\u线程\u提供\u未来\u延续 #定义BOOST\u线程\u提供\u未来\u何时\u所有\u何时\u任何 #包括 使用名称空间boost; int main(){ shared_future fa=async([](){sleep(1);返回123;}); shared_future fb=async([](

我有大量的任务要用
boost::shared\u future
框架来执行

例如具体性,考虑图中所示。

下面是一个尝试代码:

#包括
#定义BOOST\u线程\u提供未来
#定义BOOST\u线程\u提供\u未来\u延续
#定义BOOST\u线程\u提供\u未来\u何时\u所有\u何时\u任何
#包括
使用名称空间boost;
int main(){
shared_future fa=async([](){sleep(1);返回123;});
shared_future fb=async([](){sleep(2);返回456;});
shared_future fc=async([](){sleep(5);返回789;});
自动fabc=当所有(fa、fb、fc)时;
auto fx=fabc.then([](decltype(fabc)){
std::cout正如T.C.所说,您可以通过调用
share()
member函数来共享您的未来。这样您就不需要移动两次:

#include <iostream>

#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
#include <boost/thread/future.hpp>

using namespace boost;
using boost::this_thread::sleep_for;
using boost::chrono::milliseconds;

int main() {
    shared_future<int> fa = async([]() { sleep_for(milliseconds(100)); return 123; });
    shared_future<int> fb = async([]() { sleep_for(milliseconds(200)); return 456; });
    shared_future<int> fc = async([]() { sleep_for(milliseconds(500)); return 789; });

    auto fabc = when_all(fa, fb, fc);

    auto fx   = fabc
        .then([](decltype(fabc)) { std::cout << "A,B,C has completed, computing X\n"; return 1; })
        .share();
    auto fax  = when_all(fa, fx);

    auto fz   = fax
        .then([](decltype(fax)) { std::cout << "A,X has completed, computing Z\n"; return 2; })
        .share();
    auto fcx  = when_all(fc, fx);

    auto fy   = fcx
        .then([](decltype(fcx)) { std::cout << "C,X has completed, computing Y\n"; return 3; })
        .share();

    fy.get();
    fz.get();
}
如果要“共享”,请使用
.share()
A,B,C has completed, computing X
C,X has completed, computing Y
A,X has completed, computing Z