Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I look at it differntly. There is no fundamental difference between async paradigm and threads. The only difference is where the call stack is.

It just happens that in threads, there is this magical place called stack that you can SUPER fast allocate memory for new function frame. If you have a complex program that uses many deep very deep calls to functions withing functions within functions, you dont need to deal with register allocation or even "local stack frame" for local variables allocations. You just zip in and zip out of the "thread" stack all the time, millions times per second.

The disadvantage is that if for whatever reason a deeply nested thing needs to do _something_, typically I/O, then there is no easy way to yield and allow someone else to use ALU. One option is to change stack register to some other stack and continue other stream of compute. This done by OS is called a context switch.

However, for certain problems, there is an alternative - async. In async the "stack" for local variables is more explicit, very often on the heap, and is managed differently. Every time you are calling a nested function it also needs to store the local variables somewhere, so either you emulate stack somehow (in which case this is not much differnet than normal thread) or allocate the nested function local variables space using memory allocator on heap or special memory.

This is great beause it allows you to reduce the cost of a context switch - now you don't need to change the stack pointer and save context, becasue context is by definition saved in the async block. Asnc also allows you to NOT overcommit on stack size for some future deep deep nested fucntion calls that might never happen. In async you know exactly where you can yield and can plan for it.

However it's not like these two models are fundamentally different. It's the same problem of "where the local variables live" and "how to allocate this memory". We have far bigger problem - due to historical reasons "blocking" is a thing, but this is is only because we made mistakes designing operating systems (I'm looking at you fsync, open, close, or even bpf() syscalls).

 help



I do think they’re fundamentally different, for two reasons:

1. The decision of when to switch routines is more consequential than the cost of stack or context switching. Letting the OS decide results in behavior that is consistently fair for all programs, caveat scheduler bugs. Letting user code decide results in behavior that is only as fair as a given program. Writing code in the latter environment entails wrestling with the reality that it may not run to completion unless other code in the environment lets it. That applies in async programming to a much wider range of situations than kill signals/aborts, which are the only even slightly analogous things in threaded programming.

2. Async is a subset of threaded programming (it has a hardware stack, and can call typical context-switchful code if it wants to block).


You are describing preemption and time sharing with cooperative task switching. That's more or less orthogonal to async Vs not async.

Is it? Almost all "async" systems use it to model cooperative task switching. I'm not aware of any runtimes that have the distinction of async vs. blocking work that don't do cooperative concurrency (which makes sense; cooperative concurrency is a good fit for code that is mostly IO completions hooked up to an efficient IO multiplexer).

On the other side, it's similar: blocking + preemptive multitasking (or no multitasking) are nearly always paired. Unless you wanna split hairs and say e.g. "because Golang and BEAM sometimes use singleton IO multiplexers under the hood, that makes them 'async'", which is not a useful or typically-understood description of the term. This is true in multi-process, multi-thread, and (I'd argue) many multi-actor systems as well.

I think the useful distinction around async-or-not is whether the language semantics distinguish yield/resume points or not. Like, I don't have to care if syscalls/time slices/whatever are the preemption points, but if I'm writing async code, the async boundaries are very important because they're potentially task switching boundaries.

Or are we using incompatible definitions of these terms?


POSIX RT FIFO scheduling is effectively cooperative for threads of same priority pinned to the same CPU. On the other hand fo c++ coroutines running, for example on an asio scheduler served by a thread pool can't be considered cooperatively scheduled.

Forgot about self-preempting FIFO schedulers, fair enough. Not commonly used, and commonly considered a footgun, but for better or worse it exists.

C++/asio (or tokio + threadpools, or java NIO + threadpools) is not at all preemptive, though. Cooperative doesn't mean "predictable", it means "if code decides not to yield the whole program can hang". Having a few extra threads to shuffle coroutines onto doesn't change that core characteristic.


Async also allows the stack to fork. Because it uses a linked list of stack frames instead of putting them all in sequential memory, it's relatively easy for a frame to hold links to more than one next frame and wait for all of them at once.

You also have the Golang model, where they use threads but they took care to make the threads very lightweight.


> I look at it differntly. There is no fundamental difference between async paradigm and threads. The only difference is where the call stack is.

Exactly. Even "super fast stack allocation" Vs explicit stack frame is not such a defining characteristic. There are languages that look sequential but where each "stack[1]" frame is dynamically allocated and you can get async behaviour for "free". But the allocator is a stack and you get performance comparable to a more classical language.

At the end of the day, the main difference between the two paradigms is whether continuations are visible at the surface syntax level or not: they painfully are when writing CPS code by hand, they are partially hidden in languages with explicit async syntax and completely hidden in languages that look sequential.

[1] in quotes because function invocation does not need to respect a strict LIFO stack discipline.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: