Part 4 of 7 · 2 min read
Models
In plain words: Real systems often combine both kinds. The user-level threads of a program are then mapped to kernel-level threads. Three arrangements exist, and each has a name:
| Model | Mapping | Gain | Cost |
|---|---|---|---|
| Many-to-one | Many user threads share one kernel thread | Cheap to create and switch | A blocking call stops all; no parallelism |
| One-to-one | Each user thread has its own kernel thread | A blocked thread does not stop the others; parallel on many processors | Every thread costs a kernel thread, so very many threads are expensive |
| Many-to-many | Many user threads share a smaller number of kernel threads | Parallel, and a large number of threads is possible | Hard to build, and rarely used now |
The textbook also lists a two-level variant of many-to-many that lets a single important user thread be tied to its own kernel thread.
Today the one-to-one model dominates. Linux, Windows and macOS all use it for their standard thread libraries. The reason is that the cost of kernel threads fell, and the ability to use many processors became essential, while the many-to-one model gives up exactly that. Languages that need millions of tiny tasks, such as Go, build their own user-level scheduler on top of a small pool of kernel threads, which is a modern many-to-many arrangement.
Threading model
A threading model is the rule that maps a program's user-level threads to kernel-level threads. In the one-to-one model each user thread has its own kernel thread; in the many-to-one model several user threads share one; in the many-to-many model several user threads share a smaller group of kernel threads.
Example: on Linux, a program that calls pthread_create five times gets five kernel threads, which is the one-to-one model.
Check yourself: 1. Which model lets a blocked thread leave the others running, and also lets threads run on several processors? 2. Which model has the cheapest thread creation? 3. Which model do Linux and Windows use?