News for hackers. Stuff that matters.
Re:Even in Go, concurrency is still not easy by pansa2 on Thursday September 03, 2020 @01:42AM (#24361077)
> I really don't understand why async and await is even a thing anymore

Programming with threads is hard because they can context-switch at arbitrary points. Goroutines are nothing more than (lightweight) threads.

Stackful coroutines (as in Lua) aren’t much better, because they can context-switch at any function call.

Code that uses async-await is different - it can only context-switch at points explicitly marked with `await`.

Re:Even in Go, concurrency is still not easy by diek on Thursday September 03, 2020 @02:35AM (#24361348)
async/await in the general sense doesn't have anything to do with the preemption policy of a particular language implementation. For instance, C# implement async/await as keywords on top of its existing concurrency model.

That's really what it comes down to: cooperative multi-tasking vs preemptive multi-tasking.

Windows 3.1 had cooperative multi-tasking. Everything went great until one program didn't yield control, and then the whole system ground to a halt. That should sound familiar to any Node.js developers with their event loop.

Cooperative multi-tasking also extremely complicates code that actually uses the CPU. If you look inside libuv, it even has to jump through hoops with its encryption functions by effectively calling yield() in between rounds of calculation.

Re:Even in Go, concurrency is still not easy by csharptwdec19 on Thursday September 03, 2020 @01:44PM (#24366560)
>If you look inside libuv, it even has to jump through hoops with its encryption functions by effectively calling yield() in between rounds of calculation.

I would have assumed the Yield was there to help with protect against timing pokes...

Re:Even in Go, concurrency is still not easy by unscaled on Thursday September 03, 2020 @03:31AM (#24361630)
Since stackless coroutine schedulers can often be customized, you could (and are encouraged to) launch coroutine code doing heavy performance on a thread based scheduler.
Re:Even in Go, concurrency is still not easy by jayflux on Thursday September 03, 2020 @04:29AM (#24361936)
> Programming with threads is hard because they can context-switch at arbitrary points. Goroutines are nothing more than (lightweight) threads.

Goroutines don’t switch at arbitrary points, they get swapped out during IO, networking or sleeping (they’re cooperative not pre-emtive). So they’re not like threads in that fashion they’re more closer to JavaScripts event loop (I think both node and go use libuv?). The only difference is the await syntax isn’t needed because Go “awaits” by default, that is the coroutine will block and yield the thread while it waits.

Re:Even in Go, concurrency is still not easy by pansa2 on Thursday September 03, 2020 @04:54AM (#24362073)
> Goroutines don’t switch at arbitrary points

That was only ever an implementation detail, though - you always had to program as though goroutines were preemptive. And sure enough, from Go 1.14, “Goroutines are now asynchronously preemptible” [0].

> Go “awaits” by default

Yes, the main selling point of async-await isn’t cooperative vs preemptive, it’s the explicit context switches compared to the implicit context switches of threads, goroutines and stackful coroutines.

[0] https://golang.org/doc/go1.14