gpt4 book ai didi

c++ - 如何手动创建将在析构函数中阻塞的 future

转载 作者:行者123 更新时间:2023-11-28 04:28:56 24 4
gpt4 key购买 nike

我读过 http://scottmeyers.blogspot.com/2013/03/stdfutures-from-stdasync-arent-special.html但它没有显示如何实现相同类型行为的示例 - 即 future 将在析构函数中阻塞,与异步返回的析构函数相同,但不使用任何异步。

你能举个例子吗?谢谢!

最佳答案

如果您关注该文章下方的评论线程,您会发现其他专家并不同意:

  • 不是来自 asyncfuture 应该在析构函数中阻塞。

  • 根据 Scott Meyers 对标准的解释,应该是这样。

马蒂尼奥·费尔南德斯:

I don't see why this is a requirement for all futures: this particular requirement you quote is from the requirements upon std::async, not the requirements on std::future. The only requirement on the destructor of std::future is that it "releases the shared state". It happens that the shared state from std::async adds the requirement you quote, but no other shared state in the standard library has such a requirement.

赫伯萨特:

tl;dr: Martinjo already answered this correctly -- the article is not correct, the blocking applies only to futures returned from std::async with launch policy launch::async.

Scott said: and in 30.6.8/5, we see that [...] This is a requirement for any future object, not just the ones returned from std::async calls.

这不成立。 30.6.8 是 std::async 的规范。 其中所说的任何内容都是针对 std::async 的。

但正如 Martinho 已经正确指出的那样,此异常仅出现在第 30.6.8 条“函数模板异步”中,一般不适用于 future 。

结论:我认为您无法找到您正在寻找的示例。

关于c++ - 如何手动创建将在析构函数中阻塞的 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com