News for hackers. Stuff that matters.
Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @01:19AM (#24360987)
The paradigm that Go uses only fits a very narrow case of concurrency, and actually it is quite old.

Modula-2, Concurrent Pascal, Active Oberon already had similar constructs.

Async/await are configurable, not only can you replace the task scheduler algorithms, if the type being applied on does provide some magic methods, the runtime will use them instead of the default ones.

This allows for very powerful concurrency optimizations and workflows, that still look like plain async/await calls.

Re:Even in Go, concurrency is still not easy by YetAnotherNick on Thursday September 03, 2020 @03:22AM (#24361578)
Async/await can easily be implemented through channels. I view channel as providing superset of functionality then async/await at least. Granted there are other paradigms like lock free synchronization which can not be directly be translated to channel.
Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @03:26AM (#24361597)
Something that looks like async/await and has a rather bad performance can be implemented through channels.

Nestable stackless coroutines cannot. By definition they require compiler support.

Your proposed solution will not have any of the advantages of stackless coroutines (effect typing of async functions and perfectly optimal memory use) while suffering all the performance penalty of running on top of a channel.

Re:Even in Go, concurrency is still not easy by YetAnotherNick on Thursday September 03, 2020 @03:35AM (#24361655)
Channels have close to zero performance penalty. If you are talking about goroutines, you need to come up with data or code. I found goroutines to be really cheap to create.
Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @05:26AM (#24362275)
Not true: https://www.jtolio.com/2016/03/go-channels-are-bad-and-you-s...

I'm pretty sure the implementation has seen some improvements since then. But using a channel for async await is the same as using a global mutex every time you await. That is bound to be contentious on high load.

Re:Even in Go, concurrency is still not easy by Taek on Thursday September 03, 2020 @11:30AM (#24364988)
I think today channels cost a few hundred nanoseconds. For most applications, this is 'close to no overhead'. If you are doing a really tight loop of course or have threads that bump into eachother a million times per second, you're going to want a different primitive.

But most programmers aren't going to see any overhead using channels instead of something else.

Re:Even in Go, concurrency is still not easy by unscaled on Friday September 04, 2020 @11:28PM (#24381201)
Please remember the approach is "every context switch should go through a centralized channel". This including waiting on all other channels.

I need to see some benchmark to this level to be proven the overhead is negligible.

And of course, there is still the question of WHY would you ever want to do that. What can be gained from simulating an async-await model on top of a channel.

Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @03:25AM (#24361592)
So how do you change the scheduling algorithm though channels?
Re:Even in Go, concurrency is still not easy by YetAnotherNick on Thursday September 03, 2020 @03:33AM (#24361639)
async/await and channel are independent of scheduling. Nodejs doesn't have multithread but has async/await. I find goroutines lightweight enough in my personal testing and didn't tried tweaking it.
Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @03:51AM (#24361735)
On .NET and C++ they surely are not, given that you can fine tune them either by providing new schedulers, magic methods on awaitable objects that can be either value or reference types.

So not only you can control the scheduling algorithm, you can control how the async/await state gets managed in memory and also introduce additional parameters that influence the decision when to switch tasks across owner threads.

Try to do this in Go via channels, while offering just the channel syntax to the caller.

EDIT: The only thing in common between JavaScript, Python, .NET, C++ and Kotlin async/await is the name of the primitives.

Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @05:46AM (#24362367)
Custom stackless coroutine schedulers are also available in Kotlin.

Python also also allows you to customize the event loop, I think, but overriding AbstractEventLoop. A M:N implementation would probably be inefficient though because of the GIL.

Basically almost all stackless implementations I know allow for custom scheduling. The fact that some of them can't do M:N and are restricted to 1:N is just a limitation of dynamic programming languages. There is a global interpreter state which prevents you from using multiple OS threads efficiently. You either rely on GIL (CPython and Ruby MRI) or rely on just allow only one thread (Node, Lua).

So GP is right in that not _all_ async/await implementations will allow you to fully customize your threads but I think the main point here is that stackless coroutines are practically _a prerequisite_ for enabling custom scheduling (like C# et. al allow). You cannot have this custom scheduling with stackful coroutines, because that would require the scheduler to have access to low level memory management for controlling the stack.

I've never seen a language which allows that. I guess this could be doable on C++, but I really don't see the point, since it will require a lot more effort than the current stackless C++ coroutines.

Re:Even in Go, concurrency is still not easy by pjmlp on Thursday September 03, 2020 @06:09AM (#24362502)
That is my whole point, this is not doable with Go channels alone as primitive.
Re:Even in Go, concurrency is still not easy by gpderetta on Thursday September 03, 2020 @06:56AM (#24362744)
Yes, that's not a good primitive. You need some delimited form of call/cc.
Re:Even in Go, concurrency is still not easy by gpderetta on Thursday September 03, 2020 @06:55AM (#24362741)
> I've never seen a language which allows that.

It is not really hard. You can use the exact same scheduler you would use for stackless coroutines.

For example in C++ land you can use boost.asio to schedule both stackless coroutines and boost.fibers (or whatever have you). Boost.asio predates both and doesn't care.