← Lessons

quiz vs the machine

Platinum1720

Concurrency

The Context Switch Cost

What the CPU actually pays each time it swaps one task for another.

5 min read · advanced · beat Platinum to climb

What a context switch is

A context switch is the act of saving the state of one running task and loading another's so the CPU can run it instead. The saved state includes registers, the program counter, and the stack pointer. This direct work is small but not free.

The hidden indirect cost

The bigger expense is usually indirect. Each task built up useful data in the CPU caches and address translation entries. After a switch, the new task's working set is cold:

  • Cache misses rise as the new task reloads data from slower memory.
  • Translation lookaside buffer entries may be flushed, especially when switching between processes with different address spaces.
  • Pipeline state is lost, so the CPU restarts work it had pending.

Why it matters for design

Because switches cost more than the visible register copy, designs that switch constantly pay a steep hidden tax. This is partly why lightweight user space tasks, which avoid the kernel and keep caches warmer, can outperform a design that spawns and switches many OS threads under load.

Key idea

A context switch costs the visible register save plus a larger hidden penalty from cold caches and flushed translation entries, so frequent switching quietly drags throughput down.

Check yourself

Answer to earn rating on the learn ladder.

1. What is usually the larger cost of a context switch?

2. Why can lightweight user space tasks beat many OS threads under load?