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!

42 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

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

It's basically never safe to capture in a lambda coroutine.

Isn't it okay if you pass this by value in C++23? Then the lambda closure object is copied into the coroutine frame where the compiler can preserve it across suspension.

int i = 2;
auto coro = [i](this auto) -> Task<int> { co_await executor.schedule(); co_return i; }();

I've been using this on a project for a month now, and the sanitizer hasn't gone off yet. It's just two keywords, uses a big C++23 feature people will need to learn anyway, and can be enforced with a clang-tidy pass, so it seems worth it to recover the massive convenience of lambdas. But it contradicts a million guides and FAQs saying never to use coroutine lambdas, so I'd like some reassurance this is actually safe.

For C++20, I think you can also use lambda captures if you co_await the coroutine in the same line. But don't quote me on that one, and maybe that's too risky even if correct.

int i = 2;
auto res = co_await [i]() -> Task<int> { co_await executor.schedule(); co_return i; }();

Regarding your second point,

if you need to access data from the parent, and the parent awaits for the child to complete

Obviously if you pass a reference to an object, it needs to to outlive the task that uses it. I'm more concerned about temporaries. Consider:

std::string my_string = "Hello World";
auto coro = []() -> Task<std::string> {
    co_await executor.schedule();
    co_return str | std::views::reverse | std::ranges::to<std::string>();
}(my_string);
auto reversed = co_await std::move(coro);

This seems okay because we co_await the task while my_string is still alive. However, someone could very easily change it to this:

std::string my_string = "Hello World";
auto coro = []() -> Task<std::string> {
     co_await executor.schedule();
     co_return str | std::views::reverse | std::ranges::to<std::string>();
}(my_string.substr(0, 5));
auto reversed = co_await std::move(coro);

This is UB, because the lifetime of the temporary is only extended to only lives until the first suspension point (EDIT - usually this means initial_suspend, before any of your functions body executes). So passing references seems risky, since people are accustomed to temporaries being extended living long enough in synchronous code. Is there anything I could be doing here to make the behavior safer?

1

u/trailing_zero_count async enthusiast | TooManyCooks author Feb 21 '26 edited Feb 21 '26

Isn't it okay if you pass this by value in C++23? Then the lambda closure object is copied into the coroutine frame where the compiler can preserve it across suspension.

Looks OK to me, but I haven't tested with any C++23 features yet. If true, then this is a useful workaround. Thanks for sharing! It does have a couple downsides though: 1. It's unintuitive and safety is enforced by convention; if you remove the "this auto" parameter it will still compile, but now behave wrongly. 2. It may have negative performance implications if a large lambda object with multiple captures is actually materialized on the stack and then copied into the coroutine frame, vs. just passing each individual capture as a separate parameter. The compiler may be able to elide this copy (?) but it definitely won't in debug builds.

For C++20, I think you can also use lambda captures if you co_await the coroutine in the same line.

I'm not sure about this. Theoretically it would work if the lambda object was implicitly passed by reference as a coroutine parameter, and reference lifetime extension keeps the lambda object alive to the end of the full-expression. However from the original GCC thread about this, they state that the lambda object is passed as a pointer (essentially "this" pointer), so lifetime extension would not occur.

I wasn't able to find a source that corroborates your statement or even discusses this specific edge case in detail. If you could share one, I'd love to read it. With that said, even if this does work - it's SO RISKY because then all kinds of reasonable refactors which involve breaking the lamba call and co_await expression into separate lines will break it.

the lifetime of the temporary is only extended to the first suspension point

Do you have a source for this? It doesn't make sense to me. Since the temporary is created in the parent's scope, and the parent co_awaits the entire child coroutine, there's no way for the parent to "look inside" the child coroutine to know that it can destroy the temporary after the child's first suspension point. The parent doesn't get resumed until after the entire child is completed.

(unless you are using eager coroutines - in which case the parent task actually RUNS the child coroutine directly until the first suspension point. Again, this is confusing and don't do it. There's a reason I don't offer eager coroutines in my library at all. They are too difficult to reason about, have unreliable performance characteristics, and offer weird hacks like what you've described.)

Once you remove eager coroutines from the picture and think only of coroutines as "a function that returns an object" (where the object is a lazy coroutine that has not yet been started), the behavior is more obvious.

std::string my_string = "Hello World";
auto coro = [](const std::string& str) -> Task<std::string> {
    co_await executor.schedule();
    co_return str | std::views::reverse | std::ranges::to<std::string>();
}(my_string.substr(0, 5));
// substr is destroyed at the end of the full-expression above.
// coro contains a dangling reference to substr
auto reversed = co_await std::move(coro);

// however this version works
co_await [](const std::string& str) -> Task<std::string> {
    co_await executor.schedule();
    co_return str | std::views::reverse | std::ranges::to<std::string>();
}(my_string.substr(0, 5));
// substr is lifetime extended to the end of the full-expression, including co_await

2

u/38thTimesACharm Feb 21 '26 edited Feb 21 '26

Here's my source for using this auto, and here's a thread with people successfully using it. I'm not making shit up. You make a good point about the extra copy.

I'm not sure about this. Theoretically it would work if the lambda object was implicitly passed by reference as a coroutine parameter

Well, this is the issue. I thought it would be cool to discuss, here on the C++ forum, how the C++ language actually works. But apparently that gets downvotes, and instead I should throw up my hands and scream NEW FEATURE BAD DON'T USE, rather than learning how these tools actually work and finding safe and effective ways for my team to use them.

My source for co_await <lambda> is the same one GCC thread you posted. From there:

GCC does not comply with the (agreed in that discussion) intent that the capture object should be treated in the same manner as 'this', and a reference passed to the traits lookup, promise parms preview and allocator lookup. I have a patch for this (will post this week)

Later:

This was a source of considerable discussion amongst the implementors (GCC, clang, MSVC) about how the std should be interpreted. The change I mention will make the lambda capture object pointer behave in the same manner as 'this'

And further down:

Avi, If we are agreed that there is no GCC bug here (the change from pointer to reference is already in the queue)

If I'm interpreting this right, the three big compilers agree the standard's intent was for the lambda closure to behave like any other temporary, and last until the end of the full expression it's part of. It seems very teachable, consistent, and reasonable to me for a lambda closure to work the same way other temporaries do.

In your second string example, there is no UB because you co_await the expression that constructs the temporary. It would make sense if lambda closures worked the same way. I promise not to write any production code based upon this assumption, and if it turns out to be true, I promise to conceal this information from my team and tell them lambda coroutines are just broken and they aren't allowed to know why.

However from the original GCC thread about this, they state that the lambda object is passed as a pointer (essentially "this" pointer), so lifetime extension would not occur.

If the lambda is created, called, and co_await'ed in the same expression, at the risk of a million downvotes, I confused as to why we need lifetime extension at all. From cppref:

All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created

Reference binding can extend this in some cases, but isn't it already enough? Once we move on from the co_await, the async task has completed, and the captures will not be used anymore.

I admit I am confused about your statement here:

 the lambda object is passed as a pointer (essentially "this" pointer), so lifetime extension would not occur

this is a pointer for mostly historical reasons. What does it have to do with lifetimes? It seems like you're saying the following would be UB, which would just be awful:

    auto arg1_is_longopt = std::string{ argv[1] }.starts_with("--");

The starts_withmethod gets a this pointer to a temporary, but that doesn't mean it's UB. Evaluation of the expression isn't done yet.

Do you have a source for this? It doesn't make sense to me?

Sorry for not being clear. When I said "only extended to the first suspension point," I meant that literally. For a non-eager coroutine, the first suspension point according to the standard is the call to initial_suspend() , before any of your function body executes.

Again, this is confusing and don't do it. There's a reason I don't offer eager coroutines in my library at all.

I'm curious if you allow the use of std::generator in your code. Async isn't the only use case for coroutines.

2

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

Not sure who downvoted you. It wasn't me; I think this conversation has been very enlightening. Thanks for sharing the sources from Seastar and helping me to understand the resolution of the GCC issue - both point to the lambda being captured by reference which would imply that the lifetime persists to the end of the full-expression.

If the lambda is created, called, and co_await'ed in the same expression, at the risk of a million downvotes, I confused as to why we need lifetime extension at all. From cppref: All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created

You're right, this isn't technically "lifetime extension". It's a different way of describing how a temporary may persist across multiple calls... that is not the same thing (but close enough in my mind that I used the term interchangeably).

I'm curious if you allow the use of std::generator in your code. Async isn't the only use case for coroutines. Nope I haven't touched generators yet. But if I did, I don't think I would be comfortable with the idea of "remember to copy the capture variable before the first suspend point".

I think you're right on all points but since I am a library author and focused on making behaviors safe and reliable, I can't recommend usage of capture in any ways. As I described in my other convo on this thread, I offer several ways to customize tasks which can easily cause the task to outlive the temporary lambda that it was created from. So my canonical advice is to avoid this pattern - because you might want to e.g. wrap the task into a group to be co_awaited together afterward.

1

u/TheMania Feb 22 '26 edited Feb 22 '26

because then all kinds of reasonable refactors which involve breaking the lamba call and co_await expression into separate lines will break it..

Most sane refactors are still fine though. If you have the lambda as a named variable, you can call and/or await it as many times as you like, and provided no coroutine outlives the lambda itself (eg by returning the Task) it's all going to be fine.

The only refactor that will give you trouble is the very code smelly one of keeping it as an immediately invoked lambda and saving only the task to a variable (your initial example). So don't do that.

1

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

In my library you can do all kinds of things with a task variable between creating it and executing it. You can customize it, fork it, detach it, group it.

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.