4
我有一個DAG的任務,我試圖執行使用boost::shared_future
框架。boost :: shared_future和多個延續when_all
例如具體,請考慮圖中所示的data flow graph。
這裏的實現代碼了嘗試:
#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;
int main() {
shared_future<int> fa = async([]() { sleep(1); return 123; });
shared_future<int> fb = async([]() { sleep(2); return 456; });
shared_future<int> fc = async([]() { sleep(5); 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;
});
auto fax = when_all(fa,std::move(fx));
auto fz = fax.then([](decltype(fax)) {
std::cout << "A,X has completed, computing Z\n";
return 2;
});
auto fcx = when_all(fc,std::move(fx)); // <---- BAD
auto fy = fcx.then([](decltype(fcx)) {
std::cout << "C,X has completed, computing Y\n";
return 3;
});
fy.get();
fz.get();
}
然而,這不工作(很明顯,因爲我在fx
調用std::move
兩次)。我想問題是 - 有沒有辦法讓when_all
和then
返回「共享」類型,以便它明智地執行?或者是任務DAG的執行超出了可以做什麼的限制?
如果你想 「共享」,使用'.share()'? –