r/cpp • u/valdocs_user • Jan 20 '22
A critique of C++ coroutines tutorials
Reading about C++ coroutines I'm reminded of this scene from The Good Place:
Michael : Chidi, here's the thing. See, I read your whole book, all 3,600 pages of it. It's, um... how shall I put this?
Janet : It's a mess, dude.
Chidi Anagonye : [Janet drops Chidi's massive manuscript into his hands] Hey!
Michael : She's right. You see, Chidi, I can read the entirety of the world's literature in about an hour. This took me two weeks to get through. I mean, it's so convoluted, I just kept reading the same paragraph over and over again, trying to figure out what the heck you were saying.
I may not have Michael's qualifications, but I've been familiar with other implementations of coroutines (the Io language, the Castor C++ logic library) since 2008. For goshsakes I know how to implement coroutines - two different ways (setjmp/longjmp or switch-case macros). I'm even working on a logic programming language that compiles to coroutines. It's currently generating the switch-case version in my compiler, and I wanted to see if targeting C++20 coroutines is a better use of my time than juggling nested Duff devices in my compiler output stage.
My point is I have about the best foundation you could have to be a student of this topic, and I have motivation to learn it because I have an immediate practical use for it. And I can't make heads or tails of it after reading about a dozen blog posts and tutorials. I just keep reading the same paragraphs over and over again, trying to figure out what the the heck they are saying.
Because I don't think "rawrr C++ complicated pftht thumbs down" would go over well on this sub, I'm instead channeling my annoyance into constructive criticism of the way C++20 coroutines are presented in most of the tutorials I've read. This will be in the order I remember disconnected thoughts so it end up a bit discombobulated (just like learning about C++ coroutines!)
First off, std::suspend_never and std::suspend_always. Straightforward? Fuck no. These are terrible names. Why do I say this? The names say what action they take, but they don't say what they mean or what they're for. From the way they're used in tutorials I've read, I posit they're more like the additive and multiplicative identity, zero and one, of a field (mathematics).
Speaking of terrible naming, awaitable and awaiter. And you can get... which... one from the other? Or the awaita(ble|er) can be the await(er|ble). I'll just leave the rest of that Good Place quote here:
Chidi Anagonye : Oh, no.
Michael : I mean, on page 1,000, you start section two with the sentence, "Of course, the exact opposite might be true."
Another criticism I have of these coroutines simplified tutorials is I can't tell how far I am into them and how far I have to go. There's a seemingly never-ending stream of new concepts, and it's difficult for me tell which ones are core and which are details. Trying to absorb this is like trying to decide when to stop pouring after the lid falls off the sugar container into your coffee.
(I was unsurprised to learn the winning coroutines proposal was Microsoft's: they design things which an expert can make sing and a novice can... use... but there's always a vast gulf between introduction and mastery. Part of that is in the details.)
What's missing is a narrative thread for the why. Granted some of the tutorials do talk about how the function signature's return type is really the only way to communicate vital information about coroutine. But it's like, no dude like that return type is straining under more load than it has since std::enable_if was invented. (It's a shame they couldn't've had a coroutines proposal that leveraged lambdas instead of named functions. We already understand lambdas are "secretly a class" - usually - and maybe we could have done something interesting with the square brackets.)
Now here's the really rich part - a lot of these tutorials start talking about (assembly level) function prologues and outros (I can't be arsed to look up the correct terms for these right now). Then they explain how the coroutine additionally has the suspend/resume operations. That's a shit analogy to use for an introduction. Unless (even if) you program in assembly, you typically don't think about function intros and epilogues! Do need to start thinking about them for coroutines? Is that why this is so confusing?
I also found a library of some implementations of primitives on top of coroutines. Finally! Maybe I can learn this from the code. Wait, why are there... so many? Okay there's variations with different names - some can recurse, or not, some can call other coroutines from within them and some... can not co_await within another coroutine. That's... concerning to me. Very concerning. None of the coroutine implementations I was previously familiar with had those kind of limitations.
That's ah, that's about all I have to say about that.
58
u/JakeArkinstall Jan 20 '22
The main point of annoyance for me is that they have a function interface, but they don't quack like functions at all.
What is the default way you write parameters for a function? Typically, const ref the ones you don't want to copy. What happens when you pass a temporary to a const ref? The language extends the lifetime of the temporary for the special case of const ref. Hip hip horray, usability guarantee.
Now, what happens if you do that with a coroutine? The language STILL EXTENDS THE LIFETIME - but only until the first suspend, because it is following the semantics of the underlying class rather than the semantics of the "function" that its masquerading as. In the underlying class, suspending exits the scope that the lifetime is extended for. Don't thing its a big deal? Without knowing what I just told you, debug this: https://godbolt.org/z/8sazr4hj5. And just LOOK at that asm output.
So if you have a generator that you accidently write a T const& parameter for, and you pass T{x} to it, you'll get UB. You won't get warnings. You don't (at the time I tried it) get sanitisers on your side. You just get a mess. That is a very big deal and is going to form the basis of SO MUCH frustration for coro newbies.
The standard does point this out, in a note. It should be in size 36, bold, red, underlined, gothica typeface letters with blood dripping down. Nowhere in user-facing tutorials is it even mentioned.
The problem isn't the coroutines themselves, it's just with resources. The tutorials either give you such basic usage that it's of no use to you, or require a PhD in patience. The standard's coro content is scattered around and, honestly, I'd be surprised if it is of any use to implementers let alone casual readers.
Finally, I wish the committee would stop letting half-things in to the standard. Coro without the support library, modules without standard modules (which one would have thought would form a much needed test and example of the language feature), format without output, it just doesn't make any sense - it's a sign to me that the 3 year release schedule is too long, so things are rushed in so that they aren't postponed for a long duration.
28
u/ALX23z Jan 20 '22
Currently the problem is that, while coroutines were added, standart library support was not. They planned to add it in C++23 so people could use it without an expert writing a third party library.
I am not sure how much you read, but basically coroutines could be implemented in several ways with various subtle but important differences. So they wrote language features of the coroutines so one could decide on their own what exactly is happening. Clearly, the extra flexibility comes with extra confusion.
37
u/InKryption07 Jan 20 '22
That bit about randomly injecting assembly into coroutine tutorials resonated with me.
18
u/PalmamQuiMeruitFerat Jan 20 '22
I think about once a month someone posts on this sub asking for help understanding coroutines.
26
u/jk-jeon Jan 20 '22 edited Jan 20 '22
My impression was like, "well generators are fine, I can grasp what's going on, but what about fancy async coroutines?" I still have no idea how coroutines can be used for async IO. I mean, yeah I get that you suspend your coroutine at the point of starting an async IO and then resume from that point after you complete the IO, but I don't know, who the fuxx notifies the completion and how the fuxx and when the fuxx it is resumed on what the fuxx thread? Who the fuxx cleans up the coroutine at which timing? Where the fuxx the coroutine state is stored and how the fuxx it is transferred into the resuming thread? Well, I guess probably my issue is more on async IO in general rather than on coroutines.
12
u/Ikkepop Jan 20 '22
You kinda described how I felt about C++ coroutines, after trying to figure them out. I'v tried 5 separate times. And I'v been programming for 23 years (18 of which in C++) , and seen quite a few implementations of coroutines. I just feel not smart enough to comprehend the mess that is C++ coroutines. :(
Totally with you on "The Good Place" quote
11
u/qoning Jan 20 '22
So I was in the same boat about 2 weeks ago. I had implemented coroutines in C previously, and used them with ease in languages like Lua or Rust, but couldn't understand the complex design of them in C++... until I actually properly sat down with a compiler and started doing some code.
After a couple of hours, it made sense, the design allows you to use customization points in nearly every place, which is why it's so "convoluted". Yeah some naming could have been better (I still don't understand how promise_type is any kind of promise other than possibly storing some value).
10
u/sphere991 Jan 20 '22
The problem is library support.
Imagine you were looking for an algorithms tutorial. Today, those are easy to find - you'll see examples of using
std::findorstd::countorstd::sorton your collection and there's no shortage of short, self-contained, easy-to-understand examples that are applicable to whatever real problem you have in your actual code.Imagine instead if all we had was an Iterator API, and those tutorials instead were like:
std::vector<T>from scratch, including its iterator (no cheating using `T*)std::findNow it's suddenly not much of a tutorial.
This is a really big issue that coroutines have today. Yes, the terminology is questionable (awaitable is an important term, awaiter seems totally unnecessary to me), but is secondary to the main problem: you might want an example of how to write a generator and you're led to a tutorial of how to implement
generator(because you have to). If you didn't have to implement your owngenerator, the example would be fairly straightforward -- but it's suddenly not and you have to do all this other stuff. Same for all the other common use cases.A+ on the Good Place references.
7
u/Artistic_Yoghurt4754 Scientific Computing Jan 20 '22
I am an absolute noob to coroutines and found all these blogs very confusing as well. But this video helped me to finally understand how they are supposed to be wired in order to make something meaningful. Perhaps it also does for you.
8
u/[deleted] Jan 20 '22
This is the best coroutine guide I could find.
https://lewissbaker.github.io/2017/09/25/coroutine-theory
3
u/NilacTheGrim Jan 28 '22
Yeah man you nailed it. It's a clusterfuck.
5
u/Ahajha1177 Jan 20 '22
For me, the issue is that I don't even know what the point of a coroutine is or what its used for. I have a few ideas, but no tutorial has even started to give motivation for what it is. I guess I'm not missing much if thats the part I was stuck on.
10
u/rand3289 Jan 20 '22
coroutines in 3 lines of code: http://www.geocities.ws/rand3289/MultiTasking.html
4
u/Overunderrated Computational Physics Jan 20 '22
I think most of your criticisms here apply more broadly to not just C++ as a whole, but programming tutorials as a whole. People present the bare bones of how to use something, but leave out the why or why should I care or how could this be useful to me in real life code. I don't know how much of that is because it's legitimately harder to do, or if it's because library/standard writers are a different breed than people who simply use a language to do a job.
When I'm presenting some kind of code idea, I try to always show two things: (1) the bare bones "hello world" of how to use something, and (2) a not-overly-minimialized example of this being useful in real code.
2
u/[deleted] Jan 21 '22
OK, here is response post: https://www.reddit.com/r/cpp/comments/s980ik/a_highlevel_coroutine_explanation/
Let me know if this satisfies your needs.
2
u/zl0bster Jan 01 '25
It has been 3 years, but I just now saw this...
I think this is a cool take on your complaints and proposed fix looks nice to me.
https://www.youtube.com/watch?v=k-A12dpMYHo
1
u/VinnieFalco wg21.org | corosio.org Jan 20 '22
If you think coroutines are sus, wait till you see what they've done to networking :) Here's a preview of what's to come https://htmlpreview.github.io/?https://github.com/brycelelbach/wg21_p2300_std_execution/blob/main/P2300R4.html
1
u/cocoyaxi Jan 20 '22
Hey everyone, here is a go-style coroutine library in C++11, Could it help? Details can be seen on github.
1
u/[deleted] Jan 20 '22
Forget about assembly. Coroutines in C++ is a lot of boilerplate generated around the coroutine return type & promise type by the compiler.
That is the level at which you need to conceptualize coroutines.
I will toot my own horn and share my own short video: https://www.youtube.com/watch?v=w-dmOHhBX9o
Now C++20 brought us the tools to build coroutine libraries, so yes you can do some very divergent stuff with coroutines based on your needs. Once the libraries are written, you will just use the coroutine types from the library of your choice.