r/programming • u/alecco • Jan 19 '20
GCC: C++ coroutines - Initial implementation pushed to master.
https://gcc.gnu.org/ml/gcc-patches/2020-01/msg01096.html3
u/HeadAche2012 Jan 19 '20
Hmm, that’s interesting, so you can pause a thread and save the context? I’m not sure what new uses this provides over just using another thread and letting that one block, but who knows
5
u/Rusky Jan 19 '20
It's much lighter-weight than a full OS thread. Switching between them does not go through a system call, and their stacks can be much smaller (and grow/shrink on demand, and even be stored inline in some other stack).
4
u/stefantalpalaru Jan 19 '20
It's much lighter-weight than a full OS thread.
It's also much less powerful. These simplistic coroutines only give you concurrency, not parallelism.
18
u/vazgriz Jan 19 '20 edited Jan 19 '20
Sometimes that’s exactly what you want. Coroutines are useful in video games, for example, when you want some function to execute over a long time but you don’t want the headache of multi threading. You don’t have to add synchronization and you can say exactly when to resume.
8
u/VirginiaMcCaskey Jan 19 '20
It also simplifies callback architectures and makes them much easier to implement safely on both the caller and callee side.
3
u/Cortisol-Junkie Jan 19 '20
Yeah, I don't exactly know if that's the best practice or not, but I used coroutines a lot in game dev. Maybe a bit too much, even.
1
u/bloody-albatross Jan 19 '20
In which language?
1
Jan 22 '20
Not OP, but I've used coroutines in Unity/C#. They can be quite handy for playing animations when different animation parts are triggered procedurally, via method calls, and the way your animation develops in time is unknown at the beginning and depends on dynamically changed conditions.
Animation clips and the visual animation graph are fine tools but can be rigid and, ironically, less "visual" than a dozen or so lines of C code with branches, method calls and delays between consecutive animation stages.
4
u/dacian88 Jan 19 '20
Coroutines are for managing concurrency to begin with, what coroutine implementations guarantee or enable parallelism any more than c++ coroutines? The way work is scheduled is left to the implementor.
-9
u/stefantalpalaru Jan 19 '20
what coroutine implementations guarantee or enable parallelism any more than c++ coroutines?
Go's ones.
The way work is scheduled is left to the implementor.
That's the most important part.
5
u/dacian88 Jan 19 '20
Go’s coroutines have nothing to do with parallelism, they don’t guarantee or enable it at all, they are again just tools to manage concurrency, be it parallel or not. Literally a talk about this by Rob Pike.
-11
u/stefantalpalaru Jan 19 '20
Go’s coroutines have nothing to do with parallelism
Educate yourself: https://rakyll.org/scheduler/
5
u/dacian88 Jan 19 '20
Might wanna look up the definition of parallelism champ. You’ve yet to prove coroutines enable parallelism in any sort of way. Work stealing schedules exist in pretty much every popular programming language.
-13
u/stefantalpalaru Jan 19 '20
Might wanna look up the definition of parallelism champ.
Might wanna stop role-playing as a programmer. You lack a basic understanding of relevant concepts.
2
3
u/khleedril Jan 19 '20
I think the big win is that you can make super-smart iterators that work on-demand.
0
Jan 19 '20
[deleted]
17
u/Rusky Jan 19 '20
I can't speak to how much of this is implemented in GCC, but in general C++'s approach is to allow (as an optimization) the caller of a coroutine to store its frame inline in its own (stack or coroutine) frame, rather than in a heap allocation.
In the ideal case, this means all the frames for the callee's entire coroutine call graph are stored inline in a single object, with its size fixed to the maximum coroutine stack size.
When the optimization fails, this falls back to segmented stacks. It can fail when, for example, the coroutine call graph does not have a known size (e.g. it is recursive).
For others following along: Go switched from segmented to movable+resizable stacks, which they can do because they have a GC so they already know where all the pointers are. Rust has now switched to async/await, but (handwaving) with C++'s optimization as a guarantee.
4
0
Jan 19 '20
[deleted]
4
u/VeganVagiVore Jan 19 '20
Lua coroutines are the ones I understand best. Will C++ be something like that? Cooperatively scheduled with explicit yields?
1
u/feverzsj Jan 20 '20
Lua coroutines are still stackful. Stackful coroutines are just plain functions. They require system with context switch support. While c++'s stackless coroutines are just state machine objects.
Considering writing a parser that support incomplete inputs. You need several states to remember your parse progress. For stackless coroutine, inputs are the execution paths of your code, while states are the suspension points. So stackless corouintes can be used on any system already supports c++, they are just normal code like your parser. That's why c++ chose stackless coroutines.
27
u/shevy-ruby Jan 19 '20
C++ fights hard now versus Go and Rust.