context switch คือการที่ kernel บันทึกสถานะ CPU ของเธรดที่กำลังทำงานอยู่ (รีจิสเตอร์, program counter, stack pointer) แล้วกู้คืนสถานะของอีกเธรดหนึ่ง เพื่อให้ core เดียวสลับ (multiplex) หลายเธรดได้ มันเกิดขึ้นทุกครั้งที่ scheduler แย่งงาน (preemption), ทุก blocking syscall, หรือทุก interrupt — และมันไม่ฟรี
ต้นทุนซ่อนอยู่ที่ไหน
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
