【发布时间】:2020-02-19 21:23:01
【问题描述】:
我有一个线程函数可以返回外部 IP,并认为我可以将它与 std::future 一起使用:
std::future<std::string> GetIP4;
GetIP4 = std::async([]() -> std::string
{
return GetWanIP();
});
稍后在我调用GetIP4.get() 的代码中,我得到一个std::string。但是这会清空对象,所以下次我尝试从另一个线程调用 get 时它会崩溃。
这是std::future<>::get() 的预期行为吗?这是“我只有一次”的场景吗?我在documentation 中没有找到任何内容。
【问题讨论】:
-
你在找
std::shared_future -
是的,这是设计的行为。在您链接到的页面中,请特别注意
valid状态及其变化方式。
标签: c++ visual-studio visual-studio-2019