[英]std::future<>::get empties the result
I have a threaded function that would return the external IP, and thought I could use it with std::future
:我有一个线程函数可以返回外部 IP,并认为我可以将它与std::future
:
std::future<std::string> GetIP4;
GetIP4 = std::async([]() -> std::string
{
return GetWanIP();
});
When later in the code I call GetIP4.get()
, I get a std::string
.当我稍后在代码中调用GetIP4.get()
,我得到一个std::string
。 However this empties the object, so the next time I try to call get
from another thread it crashes.但是这会清空对象,所以下次我尝试从另一个线程调用get
它会崩溃。
Is this the intended behaviour of std::future<>::get()
?这是std::future<>::get()
的预期行为吗? Is it a scenario "I have it only once"?是“我只有一次”的场景吗? I didn't find anything in the documentation .我在文档中没有找到任何内容。
From cppreference on std::future<T>::get
:从std::future<T>::get
上的 cppreference :
Any shared state is released.任何共享状态都被释放。
valid()
isfalse
after a call to this method.调用此方法后,valid()
为false
。
And just before mentions that就在之前提到
The behavior is undefined if
valid()
isfalse
before the call to this function.如果在调用此函数之前valid()
为false
,则行为未定义。
Since valid()
must be true
to call get()
and get()
causes valid()
to become false
, then it isn't possible to get()
the same future
result more than once.由于valid()
必须为true
才能调用get()
并且get()
导致valid()
变为false
,因此不可能多次get()
相同的future
结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.