r/cpp • u/rhidian-12_ 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
View all comments
Show parent comment
2
u/trailing_zero_count async enthusiast | TooManyCooks author Feb 21 '26
I have a more detailed breakdown on the answer to these questions here (for my implementation): https://fleetcode.com/oss/tmc/docs/v1.4/task.html#rules-for-safe-usage-of-coroutines
It's basically never safe to capture in a lambda coroutine. This is arguably a defect in the standard since the way it behaves is so non intuitive. Because a non-capturing lambda is no different from a named function you might as well just use a named function for clarity. I said basically never because it's technically safe to access the capture in an eagerly executed coroutine (initial_suspend returns suspend_never) prior to the first suspension point. But relying on this behavior is very likely to get you in trouble later.
Accessing external references is totally fine as long as you use structured concurrency - if you need to access data from the parent, and the parent awaits for the child to complete, then there will be no issue.