#include <iostream>
#include <string>
#include <thread>
#include <future>
int main()
{
auto pms = std::promise<std::string>();
auto ftr = pms.get_future();
std::thread([&](){pms.set_value("hello world");});
ftr.wait();
std::cout << ftr.get() << std::endl;
return 0;
}
根据this link,std::future::wait
阻塞,直到结果变得可用。
为什么没有未来::等待()块
但是,上面的代码不能打印任何东西。显然主线程在完成pms.set_value
的线程之前已经完成。
为什么ftr.wait()
阻止?
我建议你看一看的std ::异步 – LeDYoM