#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <future>//std::async
// multi-threading.
// chect out: DEBUG->WINDOWS->PARALLEL STACKS.
static std::mutex m;
std::vector<std::future<void>> futures;
void DoSomeWork(void)
{
// do some work.
std::lock_guard<std::mutex> lock(m);
// some other repetitive work.
std::cout << "repetitive work\n";
}
void LoadMeshes(void)
{
std::ifstream stream("test/Model.txt");
std::string line;
std::vector<std::string> meshFilepaths;
while (std::getline(stream, line))
meshFilepaths.push_back(line);
for (const auto&file : meshFilepaths)
{
// make it parallel.
futures.push_back(std::async(std::launch::async, DoSomeWork));
}
}
int main(void)
{
LoadMeshes();
std::cin.get();
}
【Cherno的C++视频】How to make C++ run faster with std::async
最新推荐文章于 2024-09-16 16:07:55 发布