可以std :: Future Cavus Coledump而无需或等待
void func() {
std::future<int> fut = std::async(std::launch::async, []{
std::this_thread::sleep_for(std::chrono::seconds(10));
return 8;
});
return;
}
假设我有这样的功能。 std :: future&lt; int&gt;
的对象 fut
使用 std :: async
job初始化,将来会返回整数。但是 fut
将在函数 func
返回之后立即发布。
是否存在这样的问题:当 std :: async
作业返回并尝试将 8
分配给对象 fut
,该对象已经已经存在被释放了...
如果是这样,可能会发生关于Sigsegv的核心...
我对吗?还是 std :: Future
有一些避免此问题的机制?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用
std :: async
创建的期货具有阻止驱动器,以等待任务完成。即使您从
std :: Promise
中创建未来,〜Future()
不要提及您要问的问题。文档经常使用“共享状态”一词,这意味着承诺和未来共享一个(堆分配给?)状态,该状态具有返回价值,直到两个人都死亡之前才被摧毁。
Futures created with
std::async
have a blocking destructor that waits for the task to finish.Even if you instead create a future from
std::promise
, the docs for~future()
don't mention the problem you're asking about.The docs use the term "shared state" a lot, implying that a promise and a future share a (heap-allocated?) state, which holds the return value, and isn't destroyed until both die.