|
| 1 | +# 协程 |
| 2 | + |
| 3 | +- C++20 |
| 4 | + |
| 5 | +## Coroutine Handles |
| 6 | + |
| 7 | +```cpp |
| 8 | +#include <concepts> |
| 9 | +#include <coroutine> |
| 10 | +#include <exception> |
| 11 | +#include <iostream> |
| 12 | + |
| 13 | +struct ReturnObject { |
| 14 | + struct promise_type { |
| 15 | + ReturnObject get_return_object() { return {}; } |
| 16 | + std::suspend_never initial_suspend() { return {}; } |
| 17 | + std::suspend_never final_suspend() noexcept { return {}; } |
| 18 | + void unhandled_exception() { } |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +struct Awaiter { |
| 23 | + std::coroutine_handle<> *hp_; |
| 24 | + constexpr bool await_ready() const noexcept { return false; } |
| 25 | + void await_suspend(std::coroutine_handle<> h) { *hp_ = h; } |
| 26 | + constexpr void await_resume() const noexcept { } |
| 27 | +}; |
| 28 | + |
| 29 | +ReturnObject counter(std::coroutine_handle<> *continuation_out) { |
| 30 | + Awaiter a{continuation_out}; |
| 31 | + for (unsigned i = 0;; ++i) { |
| 32 | + co_await a; |
| 33 | + std::cout << "counter: " << i << std::endl; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void main1() { |
| 38 | + std::coroutine_handle<> h; |
| 39 | + counter(&h); |
| 40 | + for (int i = 0; i < 3; ++i) { |
| 41 | + std::cout << "In main1 function\n"; |
| 42 | + h(); |
| 43 | + } |
| 44 | + h.destroy(); |
| 45 | +} |
| 46 | +``` |
| 47 | +
|
| 48 | +        C++20 使用 `co_await` 将当前协程挂起时,当前的状态、变量等数据都会绑定在堆中,并创建了一个可调用对象用于后续唤醒当前协程以恢复执行。这个可调用对象的类型,就是 `std::coroutine_handle<>`。 |
| 49 | +
|
| 50 | +        当执行 `co_await a;` 时,需要 `a` 的类型必须支持一些指定的方法,而更常见的场景是,需要将 `a` 视为一个 **awaitable** 的对象或一个 **awaiter**。 |
| 51 | +
|
| 52 | +        `await_ready` 接口实际属于一个优化接口,每次执行 `co_await` 时,先执行此接口来判断是否要将当前协程挂起。当然,也可以在 `await_suspend` 中补充逻辑达到相同的优化目的,但需要注意的是,在调用 `await_suspend` 之前,编译器就已经需要将协程状态等数据绑定到可通过 coroutine_handle 访问的堆对象了,而这一操作本身就属于是「重量级」操作了。 |
| 53 | +
|
| 54 | +        由此可知,`std::await_always::await_ready` 总是会返回 `false`,而 `std::await_never::await_ready` 则总是会返回 `true`,而这两个类型的其他接口内都没有任何代码逻辑。 |
| 55 | +
|
| 56 | +## The coroutine return object |
| 57 | +
|
| 58 | +```cpp |
| 59 | +
|
| 60 | +struct ReturnObject2 { |
| 61 | + struct promise_type { |
| 62 | + ReturnObject2 get_return_object() { |
| 63 | + // Uses C++20 designated initializer syntax |
| 64 | + return {.h_ = std::coroutine_handle<promise_type>::from_promise(*this)}; |
| 65 | + } |
| 66 | + std::suspend_never initial_suspend() { return {}; } |
| 67 | + std::suspend_never final_suspend() noexcept { return {}; } |
| 68 | + void unhandled_exception() { } |
| 69 | + }; |
| 70 | +
|
| 71 | + std::coroutine_handle<promise_type> h_; |
| 72 | + operator std::coroutine_handle<promise_type>() const { return h_; } |
| 73 | + // A coroutine_handle<promise_type> converts to coroutine_handle<> |
| 74 | + operator std::coroutine_handle<>() const { return h_; } |
| 75 | +}; |
| 76 | +
|
| 77 | +ReturnObject2 counter2() { |
| 78 | + for (unsigned i = 0;; ++i) { |
| 79 | + co_await std::suspend_always{}; |
| 80 | + std::cout << "counter2: " << i << std::endl; |
| 81 | + } |
| 82 | +} |
| 83 | +
|
| 84 | +void main2() { |
| 85 | + std::coroutine_handle<> h = counter2(); |
| 86 | + for (int i = 0; i < 3; ++i) { |
| 87 | + std::cout << "In main2 function\n"; |
| 88 | + h(); |
| 89 | + } |
| 90 | + h.destroy(); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +        在 `promise_type` 定义中,可以使用静态方法 `coroutine_handle::from_promise` 来获取协程处理程序(coroutine handle)。 |
| 95 | + |
| 96 | +        与示例 1 的代码不同,示例 2 中不再需要使用 awaiter (await_suspend)来保存 coroutine_handle 了,所以这里可以直接使用 `co_wait std::suspend_always{}` 来实现相同的挂起协程的效果。 |
| 97 | + |
| 98 | +        此外,尽管在 main2 函数的第一行,通过赋值操作会使得 counter2 返回的协程对象离开函数体被“销毁”,但我们可以将 coroutine_handle 理解为指针,也就是说尽管旧的指针对象已经被销毁了,但我们依然用 h 缓存了指针对象的值,我们依旧可以通过 h 来访问其指向的协程对象,因为这个协程对象本身是不会被销毁掉的。这也就是说,调用 counter2 的同时,需要显示调用 `h.destroy()` 来释放掉这段协程对象空间,否则会存在内存泄漏问题。值得一提的是,如果这里没有使用 h 来“接收” counter2 的返回值,那么就已经存在内存泄漏的问题了。 |
0 commit comments