r/cpp • Coroutines4Life • Feb 21 '26

Implementing your own asynchronous runtime for C++ coroutines

Hi all! Last time I wrote a blog post about writing your own C++ coroutines. Now, I wanted to highlight how to write your own C++ asynchronous runtime for your coroutines.

https://rhidian-server.com/how-to-create-your-own-asynchronous-runtime-in-c/

Thanks for reading, and let me know if you have any comments!

41 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/TheMania Feb 22 '26

And that's all likely fine, as long as the task doesn't outlive (or at least execute) outside of the lambdas lifetime.

Eg provided the lambda is a named variable in the same scope as you await it, there's no problem to be had.

0

u/trailing_zero_count async enthusiast | TooManyCooks author Feb 22 '26

Sure, if you write this, it works:

int i = 0;
auto f = [i]() -> task<int> { co_return i + 1; };
auto t = f();
customize(t);
co_await t;

But nobody wants to write code like that. If you make the below seemingly reasonable refactor to reduce verbosity, it breaks. Having safety be enforced by convention only is not OK.

int i = 0;
auto t = [i]() -> task<int> { co_return i + 1; }();
co_await customize(t);

1

u/TheMania Feb 22 '26

Well it's the way I write most of mine these days, it has the advantage that you can spawn multiple via the same task factory, perhaps parameterised differently. I'll write a few lambdas at the top of the scope, and then the actual body referring to the named functions.

I'll typically give them meaningful names, of course.

If you make the below seemingly reasonable refactor to reduce verbosity, it breaks.

To me that looks like a dangerous refactor, as there's no this auto in an IIFE, but agreed. It's a pain that convention has to be relied on to use them safely. Whether that's to use named lambdas, this auto, to await in the same expression, or to not use stateful lambdas at all.

2

u/38thTimesACharm Feb 22 '26

Whether that's to use named lambdas, this auto, to await in the same expression, or to not use stateful lambdas at all.

Yeah, that's what I was thinking. It's convention no matter what you do. The only question is which convention to choose.

I like this auto the best for C++23, because:

  • Can be enforced with a clang-tidy pass
  • Keeps all of the benefits of lambdas, so people aren't tempted to refactor the code for readability, conciseness....etc.
  • Conspicuous and unusual. Two highlighted keywords in a place you've never seen them before are clearly doing something important, and anyone who removes that just because they don't know what it does should probably be fired

2

u/TheMania Feb 23 '26

Same, my one exception to it has been when I reuse the lambda in the same scope, dropping this auto to reduce coroutine state size (embedded targets, so always like to save where I can).

There is one other workaround approach that's maybe worth a consideration:

task<void> do_something() {
  co_task led_blinker{[&] -> task<void> {
    for (;;) {
      toggle_red_led();
      co_await 500ms;
    }
  }};
  log("starting something")
  co_await 1s:
  ...

ie a straightforward wrapper for "background helpers" for use within coroutine bodies.

It prevents misuse by only accepting invocables that return a coroutine, vs taking a coroutine itself, caches that invocable to a member, invokes that member and saves the resulting coroutine to another member. If you change co_task to auto compilers will typically warn that the statement has no effect, so probably reasonably refactor proof too.

This is on an eager coroutine model, but you get the idea - I've been playing with it a bit lately and tbh quite like it in this niche.

0

u/38thTimesACharm Feb 22 '26 edited Feb 23 '26

Having safety be enforced by convention only is not OK.

Isn't it also a "convention" to use parameters instead of captures? Someone might find it reasonable to refactor this:

int i = 0;
auto t = [](int i) -> task<int> { co_return i; }(i);

Into this, to save the duplication of i:

int i = 0;
auto t = [i]() -> task<int> { co_return i; }();

Likewise, I feel like "avoid lambdas that are coroutines" is also a convention. Because someone unfamiliar with the rule could think it's a good idea to change this:

task<int> int_task(int i) { co_return i; }

// lots of code...

  int i = 0;
  auto t = int_task(i);

Into this, to keep related code together and more readable:

int i = 0;
auto t = [i]() -> task<int> { co_return i; }();

My point is, while I respect your position, I don't see much difference between one set of "do this and not that" rules and another set of "do this and not that" rules. In both cases, it's possible to refactor the code in reasonable-looking ways that break. When writing C++, the end user has to think about safety with every change they make. Following some rules they don't understand the purpose of doesn't cut it.