IT lexicon › Programming › Context switching

Context switching

Programming På svenska → Updated: 2026-11-16

The OS pause a running process or thread, saves its state (registers, stack pointer, instruction pointer), and starts another.

Context switching is fundamental to multitasking. A CPU cannot truly run multiple things at once (on one core), but by switching between them rapidly, the illusion of parallelism emerges. The OS scheduler decides when to switch, often via timer interrupts that preempt a thread after its time quantum expires.

Context switches are expensive: register contents must be saved and restored, cache entries become invalid, and the TLB (translation lookaside buffer) is flushed. Each switch costs thousands of CPU cycles and impacts overall system performance. That's why tight loops and critical sections try to minimize switches by disabling interrupts or letting a thread run longer.

← Back to the lexicon