News for hackers. Stuff that matters.
Re:My tutorial and take on C++20 coroutines by creedor on Monday February 22, 2021 @02:50AM (#26221907)
I'm a bit confused by all of this. The idea of coroutines existed since the 60's. Then we came up with OOP that tries to combine function + data. And now we abolished this, are back at only functions and are now solving the 'state capture' problem for functions again with: Coroutines? How does the history of programming paradigms/patterns make any sense? :)

Good article though.

Re:My tutorial and take on C++20 coroutines by OskarS on Monday February 22, 2021 @03:19AM (#26222077)
Your analysis of history there is a bit lacking. Coroutines didn't go out of fashion because of OOP, it went out of fashion because of structured programming and higher level languages.

Coroutines are doable if you're programming directly in assembly, but if you want to do it in C-like structured languages, it turns out that it's really tricky: the whole concept of those languages are about having a single stack and hierarchical lexical structure. You can do coroutines in languages like this, but unless you want to do nasty things with longjmp and manually manipulating the stack pointer, they simply aren't built for it. You can build structured languages with first-class support for constructs with coroutines, but it took a couple of decades of programming language development for that to happen. Even today, most of the languages that support coroutines (C++20 included), only has support for the stackless kind. Basically the only languages with full stackful coroutine support in wide use are Lua and (sort-of) Go.

Re:My tutorial and take on C++20 coroutines by gpderetta on Monday February 22, 2021 @04:35AM (#26222496)
There is no particular difficulty in having one-shot continuations in C (or C++) and in fact over the last few decades there have been plenty of libraries implementing them. They just never caught on with the general C and C++ programming population, although they were popular in some niches.

C with Classes had them from the beginning, as Stroustrup liked them in Simula, but then (like many other things) had to take them out of the language after user feedback.

Re stackless coroutines and language support, while it is relatively straightforward to have stackfull coroutines as a library, the stackless kind in practice needs to be implemented as a language feature.

Re:My tutorial and take on C++20 coroutines by spc476 on Monday February 22, 2021 @04:21AM (#26222409)
Or take advantage of the ABI of the runtime, and use assembly. [1] Yeah, not portable. But using setjmp()/longjmp() has issues as well (way too easy to mess it up because it doesn't do what you think it's doing).

[1] https://github.com/spc476/C-Coroutines

Re:My tutorial and take on C++20 coroutines by diskmuncher on Monday February 22, 2021 @05:19AM (#26222812)
> Basically the only languages with full stackful coroutine support in wide use are Lua and (sort-of) Go.

Aren't Javascript Generators also coroutines?

Re:My tutorial and take on C++20 coroutines by gpderetta on Monday February 22, 2021 @07:36AM (#26223772)
they are, as far as I know, stackless. I.e. you can only yield from the generator top level frame.
Re:My tutorial and take on C++20 coroutines by sheenobu on Monday February 22, 2021 @03:04AM (#26221986)
The glib answer is that these kinds of things are cyclical.

But C++ coroutines don't seem too incompatible with the level of OOP you already get in C++, no?

Re:My tutorial and take on C++20 coroutines by jcelerier on Monday February 22, 2021 @03:47AM (#26222228)
> . And now we abolished this,

... no. coroutines are not supposed to be used in, like, more than an extremely small fraction of your codebase if you want to keep your code understandable

Re:My tutorial and take on C++20 coroutines by oivey on Monday February 22, 2021 @04:15AM (#26222372)
Eh, I think coroutines are a convenient way to achieve concurrency and parallelism. If you limit mutation and try to reduce the long reaching accessibility of variables then I think they’re generally very understandable, especially compared to other concurrency/parallelism paradigms.
Re:My tutorial and take on C++20 coroutines by jcelerier on Monday February 22, 2021 @06:39AM (#26223370)
> Eh, I think coroutines are a convenient way to achieve concurrency and parallelism.

but how often do you need concurrency and parallelism outside of handling network requests and performing some complicated math algorithm (which may be in a lib that you don't touch anyways, e.g. FFT>) ?

e.g. most UI code has to run on the main thread due to macOS / Windows limitations, and in most UI software it is extremely rare to have the kind of chains of callbacks whose readability gets improved by coroutines.

Re:My tutorial and take on C++20 coroutines by gpderetta on Monday February 22, 2021 @09:24AM (#26224861)
Coroutines are not really for parallelism. I doubt you'll see them much around math heavy code. Possibly if someone implements some Cilk style work stealing executor...
Re:My tutorial and take on C++20 coroutines by pjmlp on Monday February 22, 2021 @10:21AM (#26225506)
Ideally, but they taint everything they touch, that is why in C# I always start them from some point with Task.Run(), or leave it for the event and request handlers.
Re:My tutorial and take on C++20 coroutines by jussij on Monday February 22, 2021 @03:08AM (#26222011)
These are features built into the language designed to make multi-threaded code safer and easier to write.
Re:My tutorial and take on C++20 coroutines by Mikhail_Edoshin on Monday February 22, 2021 @03:04AM (#26221989)
Objects and coroutines are very closely related.