【发布时间】:2012-02-28 21:59:07
【问题描述】:
考虑一下我有 Lamba foo,它只是做一些事情,不需要返回任何东西。
当我这样做时:
std::future<T> handle = std::async(std::launch::async, foo, arg1, arg2);
一切运行良好,lamba 将在新线程中生成。
但是,当我不存储std::async 返回的std::future 时,foo 将在主线程中运行并阻塞它。
std::async(std::launch::async, foo, arg1, arg2);
我在这里错过了什么?
【问题讨论】:
-
也许
async返回的future会被立即销毁。如果future的析构函数内部有隐式等待,我不会感到惊讶。
标签: c++ multithreading asynchronous