IT lexicon › Programming › Critical section

Critical section

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

The stretch of code that touches shared data and may therefore only be executed by one thread of control at a time.

Edsger Dijkstra posed the problem in 1965: several processes share a variable, and none may enter while another is inside. On a regular OS the answer is a lock — a mutex when the wait may be long, a spinlock when the section is a handful of instructions. Windows went as far as naming its lightweight lock CRITICAL_SECTION.

In single-core firmware the competitor is usually an interrupt, and no lock helps there: the interrupt handler cannot wait for the main loop. Instead you disable interrupts (__disable_irq(), or cpsid i on ARM), do the work and re-enable them. Every microsecond spent with interrupts off goes straight onto the system's interrupt latency, so keep it short — and save and restore the previous state rather than blindly re-enabling, or a nested section will pull the rug from under the outer one.

← Back to the lexicon