C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Automatic Coroutine Cancellation

From: Zhao YunShan <dou1984_at_[hidden]>
Date: Mon, 13 Apr 2026 10:47:14 +0800 (CST)
I found some mistakes, let me fix them.


>class Router
>{

> // std::map<int , co_event> m_cancellation;
 std::map<int , async> m_cancellation;
>
>
> awaitable<int> handle_message(session& sess)
> {
> auto& cancel = m_cancellation[sess->Id()];
> defer(m_cancellation.erase(sess->id());
>
>
> auto client = co_await g_client_pool.get();
> defer(client.release(client));
>
>
> co_task _task;
> auto request_id = _task << [](auto& client) -> awaitable<void>
> {

> // auto r = client.request(...);
  auto r = co_await client.request(...);
> if (r == INVALID)
> {
> LOG(...);
> }
> }(client);
>
>
> auto cancel_id = _task << [](auto& cancel) -> awaitable<void>
> {
> co_await cancel.suspend();
> }(cancel);
>
>
> auto ret_id = co_await _task.wait();
> if (ret_id == cancel_id)
> {
> LOG("cancel");
> co_return INVALID;
> }
>
>
> LOG("success");

> // return 0;
     co_return 0;
> }
> void cancel(int id)
> {
> if (auto it = m_cancellation.find(id); it != m_cancellation.end())
> {
> it->second.resume();
> }
> }
>};
>

Received on 2026-04-13 02:47:21