Un context switch è il kernel che salva lo stato CPU del thread attualmente in esecuzione (registri, program counter, stack pointer) e ripristina quello di un altro thread, così che un singolo core possa multiplexare molti thread. Accade a ogni preemption dello scheduler, syscall bloccante o interrupt — e non è gratis.
Dove si nasconde il costo
DIRECT cost (visible, ~1–5 µs):
save/restore registers · run scheduler logic · switch kernel stacks
if PROCESS switch: also swap page tables (CR3) → FLUSHES the TLB
INDIRECT cost (often much larger):
cold CPU CACHES → the new thread's data isn't in L1/L2/L3 → cache misses to RAM
cold TLB → address translations must be re-walked
lost pipeline / branch-predictor state
