c++ - 是否有必要在 std::coroutine_handle 上调用 destroy?

标签 c++ c++20 coroutine

std::coroutine_handle 是 C++20 新协程的重要组成部分。例如,生成器经常(总是?)使用它。在我看到的所有例子中,句柄都是在协程的析构函数中手动销毁的:

struct Generator {
    // Other stuff...
    std::coroutine_handle<promise_type> ch;

    ~Generator() {
        if (ch) ch.destroy();
    }
}

这真的有必要吗?如果是,为什么 coroutine_handle 还没有完成,是否有 RAII 版本的 coroutine_handle 以这种方式运行,如果我们省略销毁调用?

例子:

  1. https://en.cppreference.com/w/cpp/coroutine/coroutine_handle (感谢463035818_is_not_a_number)
  2. C++20 标准在 9.5.4.10 示例 2 中也提到了它(在 N4892 上检查过)。
  3. (德语)https://www.heise.de/developer/artikel/Ein-unendlicher-Datenstrom-dank-Coroutinen-in-C-20-5991142.html
  4. https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html - 提到如果不调用它会泄漏,但没有引用标准中的一段话或为什么不在 std::coroutine_handle 的析构函数中调用它。

最佳答案

这是因为您希望协同程序的生命周期超过其句柄,句柄应该是非拥有。句柄只是一个“ View ”,很像 std::string_view -> std::string。如果 std::string_view 超出范围,您不希望 std::string 自行销毁。

如果您确实想要这种行为,围绕它创建自己的包装器将是微不足道的。

也就是说,标准 specifies :

The coroutine state is destroyed when control flows off the end of the coroutine or the destroy member function ([coroutine.handle.resumption]) of a coroutine handle ([coroutine.handle]) that refers to the coroutine is invoked.

协程状态将在完成运行后自行清理,因此它不会泄漏,除非控制没有从末端流出。

当然,在生成器的情况下,控制通常不会从末尾流出,因此程序员必须手动销毁协程。协程有多种用途,因此标准不能真正无条件地强制调用句柄析构函数 destroy()

关于c++ - 是否有必要在 std::coroutine_handle 上调用 destroy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48163475/

相关文章:

android - 关于 Kotlin 协程取消的问题

kotlin - 扇出/扇入 - 关闭结果 channel

c++ - 如何将 boost.build 项目链接到特定的静态库

c++枚举类整数不适用于数组下标

C++ header 单元导入语法

C++20 | std::is_constant_evaluate() 和 const 变量

Kotlin 协程 : concurrent execution throttling

c++ - 在C++类中声明参数构造函数时出错

c++ - 为什么C++1 1's POD "标准布局“定义是这样的?

c++ - 可以指定 C++20 模板化 lambda 来推断嵌套在参数中的类型吗?