Multithreading models visualised
Compare the mapping models and test yourself.
Multithreading models visualised
How user threads map onto the kernel threads that the CPU scheduler actually sees.
Many-to-one
N user → 1 kernel. Fast user-level switching; a blocking call blocks all; no multicore parallelism.
One-to-one
1 user → 1 kernel. True parallelism; blocking affects one thread; more kernel overhead. Linux, Windows.
Many-to-many
N user → M kernel (M ≤ N). Flexible and parallel; complex.
Two-level
Many-to-many plus the option to bind a user thread to a kernel thread.
Scenario: one thread calls read() on a slow disk
The only kernel thread blocks in read() → the other user threads cannot run either. The whole process stalls.
Only that thread's kernel thread blocks. The scheduler runs the other threads, even on other cores.
The library notices the block and schedules another user thread on a different kernel thread.
Process vs thread
| Process | Thread | |
|---|---|---|
| Address space | Own | Shared with other threads of the process |
| Creation cost | High (copy/map address space) | Low |
| Context switch | Slower (change memory map, flush TLB) | Faster |
| Communication | IPC needed | Shared variables (needs synchronization) |
| Fault isolation | A crash affects one process | A crash kills all threads of the process |
Which model gives no parallelism on a multicore CPU?
Which model does Linux use for Pthreads?
What do threads of the same process NOT share?