[英]Wait for async file write to finish before exiting main thread
using experimental/filesystem and std::async is there a way to ensure filewrites finish before exiting the main thread?使用实验/文件系统和 std::async 有没有办法确保文件写入在退出主线程之前完成?
code used to call the async function用于调用异步 function 的代码
std::vector<std::future<void>> img_Futures;
for (int i = 0; i < parsedResult.size(); i++)
{
img_Futures.push_back(std::async(std::launch::async, downloadImage, parsedResult[i], baseFolderPath, i));
}
What ends up happening is it writes most of the files, but then there are some files that get left at 0 bytes because (I think) the main thread exits before they have time to write to them.最终发生的事情是它写入了大部分文件,但是还有一些文件保留为 0 字节,因为(我认为)主线程在它们有时间写入它们之前就退出了。
You can call std::future::wait()
on each of the futures.您可以在每个期货上调用std::future::wait()
。 This will block until the future's result is available, ie until your downloadImage()
function has finished.这将阻塞,直到未来的结果可用,即直到您的downloadImage()
function 完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.