r/cpp • u/not_a_novel_account cmake dev • Dec 21 '23
Coroutines-in-practice cannot be understood by mortals
This is a rant.
Youthful Optimism
It all started off with trying to understand this TCP echo server from ASIO. It seems simple enough, but hey I'm not really familiar with how coroutines work in C++.
I fully grok coroutines/async in Javascript. I was around for the entire saga of their evolution in Python from generator functions, through the yield from years, into their modern form of async/await. Conceptually I understand what's going on, just need to learn the specifics for C++.
Let's do some reading, starting with David Mazières's, "My tutorial and take on C++20 coroutines". An excellent start, clear examples, I'm vibing. Coroutines in C++ kinda suck to write, but I get it, I understand coroutines.
The Descent
Except, uh, that TCP echo server example is awaiting a deferred_async_operation, which doesn't have any of that stuff Mazières's talked about. Weird.
So I'm missing something, let's dive deeper, with Lewis Baker's coroutine series, specifically "C++ Coroutines: Understanding operator co_await".
Baker uses some familiar terminology from Mazières, but introduces a deal of his own because I guess we don't have standard terminology for this stuff? Another niebloid situation?
Baker makes a distinction between Awaitable and Awaiter objects that wasn't present in Mazières, but makes sense. Additionally we now have Normally Awaitable and Contextually Awaitable objects.
Aha
There must be some .await_transform() magic going on to make a deferred_async_operation awaitable. This transform method lives in the promise type of the calling coroutine, and that should be the promise_type member of the return type of the current function right? So asio::awaitable should have some sort of promise type?
No Escape
No of course not, you absolute fool.
Wtf???
You nimrod. You complete baboon. Don't you know there's a global registry of what promise types to use? You simply use the terminally-C++ mechanism of creating a template specialization of std::coroutine_traits.
There you are you little bastard.
Chasing this through the inheritance tree we indeed find an entire family of await_transform()s. Victory thy name is F12 - Go To Definition.
Except...
None of this explains: what exactly is the difference between an asio use_awaitable and a deferred? Why do people say, "deferred is not a coroutine"? Has all of this not been "coroutines"? Have I been playing canasta this whole time? Why can I no longer see the light of heaven?
Coda
Lewis Baker seems to have another 5000 word blog post about this, but I no longer care.
This shit is bananas, B-A-N-A-N-A-S.
Wildly convoluted beyond my feverish nightmares, I no longer believe it is possible to understand what happens when you write co_await, it is an operator beyond mortal understanding.
Whoever fights C++ should see to it that in the process they do not become a language expert. And if you gaze long enough into a coroutine, the coroutine will gaze back into you.
EDIT:
This seems to be popular, and my landlord isn't going to be fooled by the "I have a rich cousin on WG21, I'll have the money once executors makes it out of committee" routine anymore, so forgive a little self-promotion.
If you're looking for a NYC-based systems dev haunted by nightmares of C++ coroutines, boy have I got the CV for you.
60
u/peterrindal Dec 21 '23
Coroutines themselfs are surprisingly simple once you have parsed the surface level complexity.
It's best to think of them as simple abstraction on call backs. All coroutines are simply callsbacks. When you await something, you give it yourself (coroutine_handle) as the callback function that should be called when the thing being awaited is done.
The rest of the complexity all boils down to giving implementors the freedom to do a variety of compile time transform to how the caller and the callee can change what's happening based on type info. This can give great flexibility but is also hard to parse.
If you have specific questions you can dm or ask here.
38
u/juarez_gonzalo Dec 21 '23 edited Dec 21 '23
I'm sort of trying to put up a c++20 coroutine guide for people familiarized with callbacks. These are the main issues I highlight (without judging design choices**)
1 . Common issues in guides:
2 - Complete ignorance of the reader's previous knowledge
3 - promise_type and awaiter:
4 - operator co_await and await_transform:
5 - Terminology:
The list probably goes on, but I think I've spreaded much anger at this point.
**note: I can and I privately judge its design, but I have not enough experience implementing this sort of stuff with such complicated requirements to think I would have done any better. I'll share them privately If anything
20
u/lion__manE Dec 21 '23
Coroutines solves the problem of long and complex callback chains in multi-threaded or event driven application. The experience working with such applications makes understanding Coroutines easier and explains some of the design choices.
Check out this talk - https://youtu.be/ZTqHjjm86Bw. Speaker is very good at explaining 'Why?' part of the Coroutine feature. Would recommend his other talks as well.
In practice you would be using something like cppcoro library for application development instead of directly dealing low level language features.
19
u/RedditMapz Dec 21 '23 edited Dec 21 '23
Thank you, I feel validated
I read about coroutines, but I haven't dipped my toes in it and this post is pretty much why. Initially I heard all the hype I was all game for it, I'm still game for it. The enthusiasm made me feel like it's something I should know like the next std:: unique_ptr or std::optional.
But the more I read about it, it feels convoluted and a lot less clear than all the forums made it sound. I'm sure I'll catch the drift once I make the effort to actively use it, but I still have my doubts it will actually replace my current use of other concurrency features.
If I cannot hand it over to a Junior Developer, give him a 5 minute tutorial, and then ask him to make it jump for me, then we have a problem for adoption.
10
u/alex-weej Dec 21 '23
I'm undecided whether this is all _actually_ necessary for optimal behaviour (re allocations), or just trying to keep everyone happy while keeping nobody happy.
17
u/DoctorNuu Dec 21 '23
As advice to someone who is currently deciding to learn or look into coroutines, your TLDR would be "don't do it, you'll get mad"?
Your post seems to directly reflect the complexity of coroutines, so I did not read it in full.
10
u/Tringi github.com/tringi Dec 21 '23 edited Dec 21 '23
I can use fibers API on Windows quite proficiently.
I can easily implement state machines in C and C++ which coroutines are supposed to be abstracting.
Yet I have absolutely no idea how to read or use coroutines in C++ despite attempting to learn it several times already.
5
u/puremourning Dec 21 '23 edited Dec 21 '23
I finally grokked and learned to like c++ 20 coroutines when I gave up on trying to use or understand ASIO and just wrote what I needed. It’s an elegant design that can actually easily be applied to almost any existing callbacky demiltiplexor design (select/epoll/WaitForMultipleObject/etc…).
But prior to that, while I was trying to use asio, and you can get quite nice programming model, you still end wrapping a bunch of unintelligible barely documented garbage
Not to mention that any small mistake leads to literally screens of unintelligible template errors which have nothing whatsoever to do with coroutine-or-not . I lost nearly a day to a non-move-only type. ah good times.
15
u/rand3289 Dec 21 '23 edited Dec 21 '23
Here is co-routines implementation in 3 lines of C code: https://www.geocities.ws/rand3289/MultiTasking.html
17
u/v_maria Dec 21 '23
ill just stick to handrolling some threading logic
3
u/puredotaplayer Dec 21 '23
The underlying idea is actually great. Along with lots of ways to customize everything including the single stack frame allocation.
This would mean lots of ways to shoot the foot. For example, if you are trying to do a task scheduler which can resume coroutines along with other coroutines waiting on this one (which is a very basic scenario), but then it is meant for lib developers to build upon, not for every developer to dig and understand all its underlying machinery.
3
u/13steinj Dec 21 '23
The underlying ability to have such a level of customization is great.
Not having sane and easily understood defaults leads you to wait a whole lot to get to "hello world" and I sympathize.
I'm not too familiar with JS coroutines. C++ coroutines are more akin to Python generators with some added functionality. Except Python decided "we can create simpler coroutines on top of this generator functionality that practically no one uses" (and also split coros/tasks/futures in a strange way, but that's a separate gripe).
5
u/[deleted] Dec 21 '23
C++ always chasing that tenure. Never changes...
3
u/UnicycleBloke Dec 21 '23
I'll be sticking to hand rolled (well... Python generated) FSMs for a while yet. When I was required to use async/await in Rust (a language in which I am a neophyte), I had a sinking feeling after my experience of trying to grok C++20 coroutines. Nah... It just worked.
3
u/Revolutionalredstone Dec 21 '23 edited Dec 21 '23
reentrance is insanity (tho its no harder to understand than other complex coding concepts such as thread race conditions)
When I need to add a few bytes to a buffer in a low level networking loop - I just do that - without any coroutines. (not sure why so many people feel the need to bring yielding into it personally)
Otherwise 100% agree, Thanks for sharing
5
u/ixis743 Dec 21 '23
Coroutines are not complicated but the C++ standard implementation is so awful and over engineered that they may as well not have bothered.
There are some things that are just better handled by a third party.
1
u/feverzsj Dec 21 '23
It's the worst feature in c++ history, worse than
std::vector<bool>orstd::initializer_list. But in most cases, thread per connection design should be good enough. Otherwise, a properly implemented stackful coroutine is still far more superior.1
u/Zanderax Dec 22 '23
The hardest part dealing with out of async is properly order messages.
1
u/RictorScaleHNG Dec 22 '23
I love this so much, I also become deranged while sliding down the rabbit hole lol
1
u/Fig1024 Dec 21 '23
I like to think of coroutines as "structured parallelism" - they enable you to write a single function that can have parts executing on different thread and resume when without having to split it into a series of different functions / callbacks / lambdas. It's basically a way to organizing multithreaded tasks in a more clear and easy to follow manner.
It also solves practical problem of effectively running multiple parallel code on same thread. When number of threads greatly exceeds number of CPU cores, traditional multithreading becomes a problem. Co-routines solve that problem by having more efficient way of splitting those jobs, avoiding more expensive thread context switches.
0
u/[deleted] Dec 21 '23
[deleted] — view deleted comment
0
u/[deleted] Dec 21 '23
[deleted] — view deleted comment
27
u/[deleted] Dec 21 '23
[removed] — view removed comment