为什么程序在std :: async完成之前不会退出?

时间:2015-12-16 16:03:45

标签: c++ c++11 asynchronous

考虑这个程序:

#include <future>
#include <thread>
#include <chrono>

int main()
{
    int a = 0;

    auto f = std::async(std::launch::async,[&]
    {
        a = 1;
        std::this_thread::sleep_for(std::chrono::seconds(5));
        a = 2;
    });

    auto result = f.wait_for(std::chrono::seconds(1));

    if (result == std::future_status::timeout)
    {
        printf("Function timedout ! %d\n", a);
    }
    else if(result == std::future_status::ready)
    {
        printf("Function finished ! %d\n", a);
    }

    return 0;
}

它按预期工作,但程序在异步任务完成之前不会退出。为什么?

0 个答案:

没有答案