C++ std::async等待时间过长

2022-05-08

以下示例是关于Cpp中包含C++ std::async等待时间过长用法的示例代码,想了解C++ std::async等待时间过长的具体用法?C++ std::async等待时间过长怎么用?C++ std::async等待时间过长使用的例子?那么可以参考以下相关源代码片段来学习它的具体使用方法。

[英]:C++ std::async wait is taking forever源码类型:Cpp
//it is perfectly normal that std::future::wait takes a long time,
//std::future::wait will wait until the corresponding async is finished

std::vector<std::future<void>> future_vals;
future_val.reserve(450);
for(size_t i = 0; i < 450; i++){ //we start 450 task, this is relatively fast
  future_val.push_back(std::async(std::launch::async, foo, i));
}

//we can do something else while the async tasks are runnings
Bar();

//we wait for all the task to be finished before we keep going,
//it's slow and in this case the execution time
//is about (450 * execTimeOfFoo / numberOfLogicalProcessors) - execTimeOfBar
for(auto& i : future_val){
  i.wait();
}

本文地址:https://www.itbaoku.cn/snippets/785464.html