【发布时间】:2015-06-20 20:20:31
【问题描述】:
我想在不同类型的任务上使用 PPL“when_all”。并为该任务添加一个“then”调用。
但是 when_all 返回带有向量的任务,所以所有元素必须是相同的类型。那么我该怎么做呢?
这是我想出的,但感觉有点像 hack:
//3 different types:
int out1;
float out2;
char out3;
//Unfortunately I cant use tasks with return values as the types would be different ...
auto t1 = concurrency::create_task([&out1](){out1 = 1; }); //some expensive operation
auto t2 = concurrency::create_task([&out2](){out2 = 2; }); //some expensive operation
auto t3 = concurrency::create_task([&out3](){out3 = 3; }); //some expensive operation
auto t4 = (t1 && t2 && t3); //when_all doesnt block
auto t5 = t4.then([&out1, &out2, &out3](){
std::string ret = "out1: " + std::to_string(out1) + ", out2: " + std::to_string(out2) + ", out3: " + std::to_string(out3);
return ret;
});
auto str = t5.get();
std::cout << str << std::endl;
谁有更好的主意?
(parallel_invoke 块,所以我不想使用它)
【问题讨论】: