是否有可能"Main end"
在所有result.get();
返回到下面的代码片段之前显示(在任何情况下)?
或"Main end"
将永远是最后一个出现?
#include <iostream>
#include <vector>
#include <future>
#include <chrono>
using namespace std::chrono;
std::vector<std::future<int>> doParallelProcessing()
{
std::vector<std::future<int>> v;
for (int i = 0; i < 10; i++)
{
auto ret = std::async(std::launch::async, [&]() {
std::this_thread::sleep_for(seconds(i + 5));
return 5;
});
v.push_back(std::move(ret));
}
return v;
}
int main() {
std::vector<std::future<int>> results;
results = doParallelProcessing();
for (std::future<int>& result : results)
{
result.get();
}
std::cout << "Main end\n";
return 0;
}