News for hackers. Stuff that matters.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Sunday May 06, 2018 @09:48PM (#17009837)
There are three main kinds of async IO.

* Event driven with callbacks (`on data`, `on error`, and so on).

* Stackless coroutines (async/await).

* Stackful coroutines (no direct semantic changes to code required).

Personally, I like stackful coroutines because the concurrency model has minimal effect on the structure of the code, i.e. it's possible to retrofit existing code without change.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by rubiquity on Sunday May 06, 2018 @11:48PM (#17010279)
As I understand it, stackless and stackful coroutines are an abstraction used on top of evented IO. They all use epoll, kqueue, etc. under the covers.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by SideburnsOfDoom on Monday May 07, 2018 @04:30AM (#17011177)
My understanding is the same - in C#, async and await are syntactic sugar for callbacks. The code is massively transformed into a state machine for the events.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Monday May 07, 2018 @09:43PM (#17018171)
Coroutines are useful in their own right, too.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by dnomad on Monday May 07, 2018 @01:00AM (#17010492)
I think stackful coroutines will always have their fans but the industry is heading towards stackless. The reality is that blocking I/O is a dangerous illusion. Any time you leave your address space you are fundamentally engaged in an asynchronous exchange. APIs that hide this from people -- even in the name of "ease of use" -- are simply inviting all sorts of unexpected behavior or worse, deadlocks. Stackful coroutines hide their blocking nature from the caller and this makes it very difficult to reason about their behavior.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by zaarn on Monday May 07, 2018 @03:03AM (#17010866)
You can get deadlocks in stackless coroutines too. The reason node.js won't deadlock is that it is single threaded.

I find stackful coroutines (ie, Go, Rust, Python Async, etc.) quite useful, they don't behave differently to normal functions and in most languages also don't require callbacks (you can exchange channels, generators, etc.).

Additionally, such actually threaded implementations of coroutines can take advantage of multiple CPU cores more easily, goroutines can swap quickly in and out of CPU threads. Any waiting action will suspend the goroutine until the IO is available. Unlike in Node.js it won't have to wait until the current synchronous action is done since it can be scheduled in another goroutine at any time (and goroutines automatically yield regularly or you put in manual yields).

There is nothing wrong with hiding the nature of the async under the hood of a sync function as long as the abstraction is clean, which in most cases it quite easily can be. For some cases you'll obviously need locks of course, but a simply RWMutex/RWLock can prevent deadlocks easily for 99% of situations where you will need it.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by dnomad on Monday May 07, 2018 @06:07AM (#17011541)
> There is nothing wrong with hiding the nature of the async under the hood of a sync function as long as the abstraction is clean, which in most cases it quite easily can be.

This is the question and I would disagree. Hiding the asynchronous nature of any given function is a dangerous illusion. Languages that promote it through stackful coroutines ultimately lead to programs that are difficult to reason about and often don't perform well because developers lack any real control over concurrency and are completely at the mercy of an opaque scheduler. (And frankly, even languages like Go which get this 'right' ... don't. Eventually even people who like Go realize Go channels are bad[1].)

The answer here I think is going to be higher kinded types a la Rust. Asynchronous is a "fundamental property of time and space" -- ignore it at your own risk. But if the type system can elegantly capture the difference between present-values and values-to-come then you can realize the best of both words: code that reads like a single-thread-of-control but is actually heavily asynchronous. This is why 'await' style stackless coroutines and Futures prove to be so popular.

Though I agree more research is needed here. I was disappointed to see the Rust developers (who apparently convinced people to pay them to research this stuff) converge so quickly on stackless coroutines.

[1] https://www.jtolio.com/2016/03/go-channels-are-bad-and-you-s...

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by zaarn on Monday May 07, 2018 @06:29AM (#17011634)
Nobody forces you to use channels in Go, I use them rather rarely, in 9 out of 10 cases it's to pass around a stop handler for some activity loop. That's where they are quite useful.

I think of myself of an older generation so my approach to concurrency is to use locks and fancy data structures and fancy architecture to avoid race conditions.

I would suggest looking into how easy it is to write an HTTP handler in Go. That involves plenty of go routines. Each connection is a go routine. Each HTTP request also gets it's own (in HTTP/2 the former and later may not be the same).

I don't have to think about the blocking nature of an operation to, for a recent example, fetch and parse a remote webpage, I simply do it. The Go runtime will take care of scheduling the goroutines such that if a new request comes in while I'm a tight loop the HTTP server can continue to handle queries.

In JS land however, I can't write tight for loops without potentially blocking up the entire server.

You mentioned the solution yourself, hide the async nature of the code. My HTTP code doesn't read like async code until you hit global resources. That's what I essentially want.

I do not want to think about async until I need it and when I need it I should be able to pretend it's sync without cognitive overhead; that enables me to efficiently reason about the steps a routine takes before every resource a request uses is released again.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @08:50AM (#17012279)
As the author of the linked post I would love to correct a misconception here.

Go is fantastic about asynchronous programming and the fact that it hides it is one of Go's strengths. Channels are/were overused when I wrote that post but that's completely independent of Go's excellent async support.

My all time favorite blog post on the matter is http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y... which is required reading about why a language hiding a program's async nature is actually the best thing it can do, and Rust's decision to not implement green threads at the language level was wrong, in my opinion.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Monday May 07, 2018 @09:49PM (#17018195)
> This is the question and I would disagree. Hiding the asynchronous nature of any given function is a dangerous illusion.

I understand where you are coming from. However, in my experience, it's "turtles all the way down". Pre-emptive multi-tasking, NUMA, multiple CPUs, CISC, all add their level of asynchronicity to your program execution whether you are aware of it or not.

The right answer in this case is "hiding the nature of async under the hood". Yet, we've seen in a few cases recently, e.g. the recent CPU bugs, where this isn't entirely possible.

> ultimately lead to programs that are difficult to reason about and often don't perform well because developers lack any real control over concurrency

My experience has been the complete opposite. It's hard to make complex parallel programs and reason about execution, but concurrency primitives like fibers and reactors are a negative overhead abstraction which makes code simpler and easier to understand and therefore allows us to do more complex things with the same cognitive potential.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by beagle3 on Monday May 07, 2018 @05:51AM (#17011467)
> I find stackful coroutines (ie, Go, Rust, Python Async, etc.) quite useful,they don't behave differently to normal functions

Python Async is "stackless coroutine", not "stackful" according to the taxonomy given by GP. They also behave quite differently from normal functions.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by xfer on Monday May 07, 2018 @03:58AM (#17011076)
Rust allows you to implement bot stackless/stackful coroutines. But the popular library(futures) is stackless and there are proposals to make stackless coroutines convenient(generators RFC).
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @08:56AM (#17012316)
Please read http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...

It illustrates decisively that a language that doesn't hide async behavior balkanizes libraries into (at least) two camps for everyone's detriment.

Fixing this and doing it correctly requires language support, yes (the language's mutexes must work right in the appropriate contexts) but it is much better for the ecosystem to hide the details of async.

Making the programmer deal with async nature (even with await, yield, etc) adds additional unnecessary details for the programmer to worry about and makes the program harder to reason about.

As for where the industry is headed, Go shows what it's like getting this right.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by dom96 on Monday May 07, 2018 @09:14AM (#17012432)
It seems to me that a simple solution to this is to default to using async for everything. That way there is no "colours", everything is a single colour (async) and everybody is happy.

Can you see a problem with this approach?

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @09:16AM (#17012447)
Nope! That's what Go does under the hood... and then eliminates keywords like await/yield and promises, because they're always implicitly there.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @09:19AM (#17012464)
On Linux, nonblocking IO has not always been great, so under the covers Go often translates calls to actually schedule blocking work on a threadpool somewhere, but from the perspective of the programmer, everything is implicitly using await/yield and promises.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by zzzcpan on Monday May 07, 2018 @09:29AM (#17012527)
What color is your function is one the most inadequate articles on concurrency ever written. Even managed to claim shared memory multithreading as superior to all other concurrency models. It's sad that it's popular, shows how little people understand about the subject.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @09:43AM (#17012617)
The article isn't about concurrency in general though. It's explicitly about the question of how coroutines are called and implemented. The article is specifically about one facet of concurrent programming that arises when you use shared memory multitasking.

I would imagine articles that focus on a small subpiece of concurrency are inadequate articles about concurrency in general, yes.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by zzzcpan on Monday May 07, 2018 @09:57AM (#17012721)
But it is. It tries to argue that other concurrency models are bad, because they do not look like shared memory multithreading. Even though other concurrency models actually aim for something different than shared memory multithreading to at least get away from its flaws.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @10:01AM (#17012767)
Where does it talk about other concurrency models at all? I just reread it to make sure I wasn't crazy.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by zzzcpan on Monday May 07, 2018 @10:12AM (#17012863)
Pretty much throughout the whole article. Callbacks, promises/futures, async/await for example all assume different concurrency model from shared memory multithreading and hence are programmed differently.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by jtolds on Monday May 07, 2018 @10:20AM (#17012936)
The article is explicitly saying that green threads are better than callbacks, promises/futures, and async/await, yes. It is definitely making a comparison between those two styles of implementations, and it is saying that (what you call) shared memory multithreading is better than callbacks.

I want to point out though that the article is not claiming OS 1:1 threading is better than async/await/callbacks. OS-level 1:1 threading is too heavy, and it's obvious that many programmers reach for callbacks to handle high concurrency evented I/O. The article is claiming that languages that allow a blocking style of programming via green threads (to still get the same high concurrency) allow a more natural way to program.

Your point was the article was one of "the most inadequate articles on concurrency ever written." I agree. It wasn't trying to be a complete article on concurrency. It says nothing about the actor model.

Then you said it "Even managed to claim shared memory multithreading as superior to all other concurrency models." The article doesn't talk about all other concurrency models. It only talks about the programming styles that "shared memory multithreading" is better than.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by dnomad on Monday May 07, 2018 @11:40AM (#17013598)
> The article is claiming that languages that allow a blocking style of programming via green threads (to still get the same high concurrency) allow a more natural way to program.

I would say this is a subjective conclusion at best. We might ask instead which model facilitates collaboration. That is in a large, very concurrent program composed by many developers which model is going to lead to more robust code. The interesting thing about M:N programming is that large programs in 1:1 languages (Java, C#) tend to converge on to the M:N model. Many large concurrent Java programs therefore end up involving several carefully monitored threadpools with well-defined contracts that describe how work gets scheduled and distributed between the threadpools. But this is not an argument for M:N languages precisely because the "problem of scheduling" often tends to be very domain specific. Other programs have lots of success with the Reactive model because they're never truly blocking and waiting for information that hasn't arrived. Some find success with Actors. It's worth considering then that different concurrency problems call for very different solutions and saying one model is better than another is erroneous.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by zzzcpan on Monday May 07, 2018 @10:51AM (#17013162)
There are different concurrency models. Shared memory multithreading that Go uses is one. Green threads, goroutines, 1:1 threads, M:N threads are all part of that model. It has synchronous APIs, concurrent memory access and lots of problems. Callbacks, promises/futures, async/await all deliberately chose a different model, that doesn't have those problems and instead are programmed asynchronously. Their whole purpose is to enable programs that only explicitly give up control. So you can't claim they are the problem if they don't allow implicit blocking, as this is the reason they exist and the way they avoid all of the problems shared memory multithreading has.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by dnomad on Monday May 07, 2018 @11:17AM (#17013353)
> It illustrates decisively that a language that doesn't hide async behavior balkanizes libraries into (at least) two camps for everyone's detriment.

That article simply begs the question. Or rather, I think the author has missed the point. Advocates of Futures-based APIs for example don't regard the balkanization as a bad thing. That's the point -- asynchronous functions really are different, they really do accept and return different types of values in the form of callbacks and futures.

Threading and shared memory don't solve anything, btw. These are implementation details. This is not about implementation so much as it's about expressing contracts between modules. Go channels are, frankly, rather useless but still, to my point, they don't try to hide asynchronous behavior. Go could've gone whole hog and provided yield and subsumption but I think they wisely realized that concurrency at scale requires shared nothing message passing. Or rather the essential insight of CSP (and Actors, and PPP and so many other models) is that reifying events and the flow of events is a good thing.

(Though I've heard it proposed that this is just because the limited human mind is "event oriented". Super intelligent martians and AIs would use Dataflow[1] languages to talk about the illusion of time. They might see the world as a giant spreadsheet.)

> Making the programmer deal with async nature (even with await, yield, etc) adds additional unnecessary details for the programmer to worry about and makes the program harder to reason about.

Again, I think it's kind of wacky to think that the async nature is "unnecessary" or accidental for the programmer. The solution the programmer is trying to model is asynchronous. The whole point is to allow the end User to do stuff while you talk to the database or process multiple trades at the same time. You can't wish this asynchronous nature of the problem away because its asynchronous nature, rather than being an unnecessary detail, is the problem. A lot of very smart people have spent a lot of time trying to "solve" concurrency by making it invisible. All failed. The issues around concurrency are a problem that must be addressed head on.

Types are the abstraction that let us be very precise for describing how a system can change. Using the type system to bring the problems of concurrency to the forefront so that they can reasoned about and enforced by the compiler is absolutely absolutely a good thing. People don't like this though because concurrent APIs are "viral". One asynchronous function now requires all callers to either block or become asynchronous themselves. But this was always the case anyways. As Morpheus would say, "How would this be different from any other day?"

[1] https://en.wikipedia.org/wiki/Dataflow_programming

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Monday May 07, 2018 @11:40PM (#17018636)
> asynchronous functions really are different, they really do accept and return different types of values in the form of callbacks and futures.

I'd argue that all code at some level is asynchronous. Once you start considering how the CPU executes code, and how OSes schedule threads/processes, it's not all that different. Yet, for some reason, we call that code synchronous.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by beagle3 on Monday May 07, 2018 @05:48AM (#17011459)
The stackful coroutine route is ideal on paper, but in practice it tends to perform poorly if you retrofit existing code -- because quite often, that existing code depends on blocking i/o or relatively long computations; e.g. opening a file, reading data from a database, or waiting for a TCP connection on the other side of the world to yield data.

It is, of course, possible to wrap all of them in a stackful coroutine interface - but that is no longer "retrofitting existing code without change". My impression is that the cumulative experience is to go either with event driven (in C / C++ / Python) or Stackless (in Python / JavaScript) or threaded (in Java), but stackful coroutines very rarely deliver on their promise.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by girvo on Sunday May 06, 2018 @10:59PM (#17010092)
What’s a good example implementation of stackful coroutines?
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Sunday May 06, 2018 @11:24PM (#17010197)
There are two kinds of stackful coroutines - fibers and green threads.

The difference is that fibers stay on the thread they were created on, while green threads can transit between OS threads. There have also been projects to cooperatively schedule OS level threads to minimise latency, but that's a bit different.

I don't know that many decent green thread implementations [off the top of my head], but an interesting one is yahns: https://yhbt.net/yahns/design_notes.txt - to me, green threads have all the benefits of coroutines but all of the downsides of multi-threaded programming.

I implemented fiber based concurrency for C++ here: https://github.com/kurocha/async. An example would be https://github.com/kurocha/async-network/blob/d1d2bf12eb2d9b... - calling read yields the fiber if it would block as implemented here: https://github.com/kurocha/async/blob/2edef4d6990259cc60cc30...

I implemented almost exactly the same model for Ruby here: https://github.com/socketry/async and if you are interested in how it works, see these projects which build on it: https://github.com/socketry/async#see-also

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by RussianCow on Sunday May 06, 2018 @11:00PM (#17010097)
Any sort of "green thread" implementation, such as Python's greenlets.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by Tarean on Monday May 07, 2018 @12:26PM (#17014033)
Haskell does async IO by default using green threads. GHC allocates Thread State Objects which completely represent a (green) thread on the heap.

A capabillity which represents a cpu core contains a queue of TSO's. If a thread blocks on something it can also move to a different queue which is used for thread synchronization, blocking on computations from other threads, io etc.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by anonacct37 on Sunday May 06, 2018 @11:19PM (#17010177)
Would golang count?
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Sunday May 06, 2018 @11:51PM (#17010290)
Yes go would fit into the green thread bucket pretty well.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ufo on Sunday May 06, 2018 @11:05PM (#17010117)
Lua coroutines work like this.
Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by samstave on Sunday May 06, 2018 @11:33PM (#17010228)
Ok.

Im going to ask you a favor, because i love this comment so much based on how info-dense it is.

I challenge you to unpack it not as an ELI5 - but ELIInfant.

Please accept. Many shall learn from such.

Re:The Various Kinds of IO – Blocking, Non-Blocking, Multipl... by ioquatix on Monday May 07, 2018 @12:26AM (#17010390)
I particularly like this video. Start watching from here: https://www.youtube.com/watch?v=_fu0gx-xseY&feature=youtu.be... for the historical context/concepts. He gives an absolutely brilliant overview, better than I can do here with a wall of text.

However, he is an advocate for stackless coroutines. They are a good idea in some situations but they still have a semantic overhead unlike stackful coroutines. They may have slightly better performance but at the expense of semantic overhead (async/await, explicit futures/concurrency).

I like to think that I'm not advocating for a specific design choice, but for a design choice which makes it possible to write higher level abstractions without the need to expose the underlying concurrency models.

If the video doesn't answer all your questions, I'd be happy to answer specific questions.