C++ Logo

std-discussion

Advanced search

resuming coroutine inside await_suspend

From: Артём Колпаков <ddvamp007_at_[hidden]>
Date: Mon, 24 Jun 2024 18:53:08 +0300
Good day. I'm learning coroutines and I don't see any clarification in the
standard about a few things.

First, can a coroutine be resumed inside an await_suspend call? I mean the
following patterns

void await_suspend(auto self) {
  self.resume();
}

bool await_suspend(auto self) {
  self.resume();
  return false; // UB here?
}

auto await_suspend(auto self) {
  auto h2 = some_handle_;
  self.resume();
  return h2;
}

These calls behave differently. If false is returned, the coroutine is
resumed twice from the same point, which (maybe) looks like an optimization
in the implementations. This happens in both gcc and clang (
https://godbolt.org/z/KM63dooae)

The second thing I'd like to find out is, is it possible to return the
handle of the coroutine itself to resume it directly? And can it be
combined with the above?

auto await_suspend(auto self) {
  // self.resume();
  return self;
}

Thank you for your attention.
Artyom Kolpakov

Translated with DeepL.com (free version)

Received on 2024-06-24 15:53:26