| 22 | } |
| 23 | |
| 24 | task<> f() { |
| 25 | // The same as std::stop_source. |
| 26 | // See https://en.cppreference.com/w/cpp/header/stop_token |
| 27 | stop_source source; |
| 28 | stop_token token = source.get_token(); |
| 29 | |
| 30 | co_spawn(g(token)); |
| 31 | |
| 32 | co_await yield(); // Let `g` starts. |
| 33 | source.request_stop(); // Trigger the callback. |
| 34 | |
| 35 | // The `source` and `token` are destroyed before `g` finished. |
| 36 | // It is safe to do that. |
| 37 | co_return; |
| 38 | } |
| 39 | |
| 40 | int main() { |
| 41 | io_context ctx; |