← Lessons

quiz vs the machine

Gold1480

Concurrency

The N to M Threading

Mapping many user level tasks onto fewer kernel threads, and why the hybrid is tricky.

5 min read · core · beat Gold to climb

Three mapping models

How user level tasks map to kernel threads comes in three shapes:

  • One to one maps each user thread to one kernel thread. Simple, but the kernel manages every thread.
  • Many to one maps many user threads onto a single kernel thread. Cheap switches, but no real parallelism and one block stalls all.
  • N to M, also called many to many, maps N user level tasks onto M kernel threads where M is smaller.

Why N to M is appealing

N to M aims to combine the best of both. The runtime schedules cheap user tasks in user space, while the M kernel threads let several tasks run in parallel across cores. You get lightweight switching and true multicore use at once.

The hard part

The trouble is coordination. When a user task makes a blocking kernel call, the runtime ideally moves other ready tasks to a different kernel thread so the core is not wasted. Doing this well needs cooperation between the user scheduler and the kernel, which is complex. Several systems tried and later simplified, but the model underlies many modern runtimes.

Key idea

N to M threading maps many cheap user tasks onto fewer kernel threads to get both lightweight switching and multicore parallelism, but balancing tasks across kernel threads on blocking is hard.

Check yourself

Answer to earn rating on the learn ladder.

1. What does N to M threading map?

2. What makes N to M hard to implement well?