简体   繁体   English

在退出主线程之前等待异步文件写入完成

[英]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.

相关问题 C ++允许后台线程在退出应用程序之前完成 - c++ allow background thread to finish before exiting application 如何在退出之前重新连接线程或等待线程完成 - How to reattach thread or wait for thread completion before exiting 让主线程等待提升线程完成任务(但未完成) - Have main thread wait for a boost thread complete a task (but not finish) C ++如何等待在另一个线程上执行的方法然后主线程完成(VS2010) - C++ how to wait for a method that gets executed on another thread then the main thread to finish (VS2010) asio - 等待异步操作完成 - asio - Wait for async operations to finish 使用pthreads优雅地退出主线程 - Gracefully exiting a main thread with pthreads 使用AWS-cpp-sdk时,等待异步调用返回,然后退出程序 - Wait for async calls to return before exiting program when using AWS-cpp-sdk 让一个线程等待另一个线程完成执行 - Making a thread wait for another thread to finish the execution 在退出关联线程之前关闭线程句柄 - Closing a thread handle before exiting the associated thread 如果返回值不是左值,为什么主线程要等待 std::async() 创建的线程? - Why does the main thread wait the thread created by std::async() if the return value is not hold by lvalue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM