Sub topic 1
Why async: cooperative concurrency compared with threads
Prerequisites
This chapter assumes you know ownership and borrowing, traits and generics, basic use of threads and Arc, and how to write a simple async fn and call .await. It does not need Pin or wakers. Later chapters cover those.
The problem: waiting is expensive with threads
Most programs that serve the network spend their time waiting. A server waits for a socket to deliver bytes, a database to answer, or a timer to fire. The CPU has nothing to do during that time.
The simplest model gives each connection its own OS thread. When a thread waits, the kernel puts it to sleep and runs another one. This is called preemptive scheduling, because the kernel can interrupt any thread at any moment.
This model works well up to a point. Each thread needs its own stack, and the OS reserves memory for it. Each switch between threads goes through the kernel. With a few hundred connections this is fine. With tens of thousands, the memory and scheduling costs start to matter.
Cooperative concurrency
Async Rust uses a different model. Many tasks share a few threads, and a task runs until it reaches a point where it cannot continue, such as waiting for I/O. At that point it hands control back. This is called cooperative scheduling, because tasks must volunteer to pause.
In Rust, the volunteering point is .await. If the awaited operation is not ready, the task pauses and the thread picks up other work. When the operation becomes ready, the task is resumed.
A paused task is stored as a small value in memory. It holds only the data it needs to continue, so it is usually far smaller than a thread stack.
Comparing the two models
| Aspect | OS threads | Async tasks |
|---|---|---|
| Scheduling | Preemptive, by the kernel | Cooperative, at `.await` |
| Memory per unit | A full stack | Only the saved state of the task |
| Switch cost | Kernel context switch | Ordinary function return and call |
| Good for | CPU-heavy work, simple code | Many tasks that mostly wait |
| Main risk | Resource limits at high counts | One task that never yields stalls others |
The last row is the key trade-off. Nothing can interrupt a task that does not reach an .await. A long computation or a blocking call holds its thread, and every other task on that thread waits. A later chapter returns to this.
What Rust adds
Rust does not build a scheduler into the language. The standard library defines the Future trait, and the async and await syntax. Running futures is left to libraries called executors. This keeps async usable without a heavy runtime, but it means you meet the machinery directly: polling, wakers, and Pin. The rest of this tutorial builds those up from the Future trait.
Exercise
A task calls a function that sleeps the whole thread for two seconds, with no .await inside it. The executor has one thread and ten other tasks. What happens to those ten tasks, and why?
Show answer
They cannot run for those two seconds. Scheduling is cooperative, so the executor only regains control when the running task reaches an .await and yields. A thread-sleeping call never yields, so it holds the only thread.