upvote
There are two schools of thought for this.

One is the poll model, you ask the OS when you can use something.

The other one is the completion model, where you ask the OS to do something on your behalf and ask it when it’s done.

In the former you are the mailbox yourself, in the latter the OS is the mailbox.

When you are the mailbox yourself threads are kind of needed. When the OS is the mailbox it can implement them however it wants to.

I’d guess this OS is doing Mailbox duty itself instead of letting userspace handle it.

reply
There is an extra step after "you ask the OS to do something on your behalf and ask it when it’s done".

That is "you ask the OS to do something on your behalf and you do not have to check when it is done", because you have a unique queue where you receive notifications about everything that is of interest for you, including the completions of I/O activities that you have requested previously.

"You ask the OS to do something on your behalf and ask it when it’s done" was an ancient method to perform asynchronous I/O, which survived in many operating systems much more years than it should. Continuing to use such a method today is a mistake.

The best method for asynchronous I/O is with a unique input queue, like in the Linux io_uring. The second best method, because it can be used to implement a unique input queue, is with what in DEC RSX-11M was called "Asynchronous System Trap Service Routine", i.e. a callback function to be invoked upon I/O completion, which was inherited from DEC RSX-11M into DEC VMS, from where it was inherited into Microsoft Windows NT, and then in 1994 MS Windows NT 3.5 introduced the "I/O Completion Ports", which was AFAIK the first improvement in asynchronous I/O handling after what DEC RSX-11M did 20 years earlier, in 1974 (which was again the first improvement over what IBM PL/I already had in 1964, when it had the equivalent of WaitForMultipleEvents, but in a more powerful variant than in Windows NT).

reply
I/O uring is built upon this, with it i can serve 155 http responses per syscall, whereas with epoll i can serve 0,2.

Having your OS do the batching for you can make it twice as fast.

My new http implementation went from 3 million responses per second per thread to 6 million, just from replacing epoll and non blocking sockets with liburing and batching.

reply
> The headline advertises 'no threads', but in the article, it mentions all actor methods are non-blocking, and async. Not blocking what?

I think the very brief answer to that is: "Processes." as in the general concept of communicating processes. How those are actually implemented on another layer depends on the platform. You can also run an actor system on a machine, that can only do one thing at a time, using a single CPU core. The work the process does is not blocking, but the platform might not have the capability to do more than one thing at a time. But this is different from work being inherently sequential.

reply
Actor methods aren't async. You can create async tasks, but there's no message pump thread. Every "await" results in a suspension via a future that gets carried as data, essentially tied to a response message. Reasoning about async from a threaded perspective won't make sense, because there are no threads. Async here just gives you a nice state machine, not a message pump.
reply
«Threads» imply «shared state» within the confines of the same process, hence the need for locking primitives, mutexes, and alike.

«Actors» imply «no shared state» – there is a message inbox and a message outbox, but the state is private to the actor.

Both can be asynchronous.

reply
I think this taxonomy is not good. It’s comparing apples to oranges, or apples to fruit sugars or something.

Threads are not a programming paradigm or style, any more than a CPU is a programming paradigm or style. They’re a tool, an existential piece of the landscape which can be used to perform paradigms/styles. Like CPUs, they do have affinity for certain use cases, but the car is not the road trip.

reply
If threads don’t share state, they are effectively the same as processes. The whole purpose of having threads in addition to processes is to share state. If you don’t share state, you don’t need threads. That’s what the blog post means by “an OS with no threads”.
reply
Yes, they are.

Threads are to a process what a single CPU core is to a multi-core CPU.

reply
There are still tons of advantages to programming with multiple threads or multiple processes even on a single-core CPU, though. Scheduling is a very powerful thing to be able to use.
reply
It’s pre-coffee, and I totally disagree “threads are not a programming paradigm”. But the best way to do that is to say that the car IS the road trip and it’s a fundamentally different thing if you use a train.
reply
Threads are an OS level concept introduced by a kernel, e.g. the Linux kernel. If you do not have an OS at all, you do not have threads (lets ignore hardware threading for the sake of the argument). You just have the core itself be occupied by the actor scheduler, but no thread scheduler.

Blocking is not a threading or even OS concept. Blocking is a CPU core and programming language concept derived from sequential processing. Blocking means that instructions behind the block are not being executed while the block is in progress.

In the actor model, sending a message must never prevent the execution of instructions that follow after sending the message. The continued execution of further instructions must be guaranteed until the actor has processed the message. Responses must be received through the actor's mailbox.

Since blocking as an absolute concept may not make much sense, because all sequential processes (e.g. execution of an instruction) are blocking temporarily, the author decided instead to define a time bound, so if mail box processing takes too long, simply because the algorithm is CPU intensive, the author considers it blocked as well and schedules the next actor.

Edit: I guess the confusion comes from the fact that Rust's async facility automatically converts the code into a state machine that is compatible with actor execution semantics, but technically violates the spirit of actors.

reply
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).

reply
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).

reply
You are describing preemption and time sharing with cooperative task switching. That's more or less orthogonal to async Vs not async.
reply
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?

reply
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.
reply
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.

reply
> 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.

reply
On ancient single-core single-thread CPUs, threads were indeed a concept introduced by the operating system, and the use of threads was completely optional.

On modern CPUs, which have many hardware threads, the operating system and the user programs must handle at least the number of hardware threads, e.g. 32 threads on an AMD 9950X.

Implementing more threads than the hardware threads is optional, but the hardware threads must have their own states and stacks and if they exchange messages the inter-thread messages must pass through message queues located in shared memory and using algorithms that enable concurrent accesses.

Naive implementations of message queues can use locks for mutual exclusion, but better implementations exploit the fact that message queues, like also arrays, can be dynamically partitioned using atomic fetch-and-add instructions, so that many threads can access concurrently a message queue, without using either mutual exclusion or optimistic accesses (which may need retries).

It is possible to have an operating system where there exists no shared state between threads, with the exception of the shared message queues.

Because message queues can be implemented without the need for mutual exclusion, it is possible to have an operating system where mutual exclusion (i.e. locks) is not used anywhere.

Whether an operating system without shared state between threads can have a performance comparable with e.g. Linux or Windows, is an open problem.

The lack of shared state does not mean that superfluous copies are needed. The messages may contain pointers to buffers in shared memory through which the bulk of the data is transferred between threads, but those buffers always have a current owner, i.e. the receiver of the message, and no other thread attempts to access them, including the sender of the message, which has written the buffer before sending the message.

reply
Modern CPUs have more processors. When you have hyperthreading you have split-brain processors. They are different from the OS concept called threads.
reply
Blocking is a threading concept. It means the thread is immediately descheduled and will not be scheduled until a certain event happens.
reply