Um context switch é o kernel salvando o estado de CPU da thread em execução (registradores, program counter, stack pointer) e restaurando o de outra thread, para que um único core possa multiplexar muitas threads. Acontece a cada preempção do scheduler, syscall bloqueante, ou interrupt — e não é de graça.
Onde o custo se esconde
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
