What is Critical section?
A part of a program that operates on shared data and must be run by only one thread at a time. It is protected by a lock and kept as short as possible.
Japanese name: クリティカルセクション
A part of a program that operates on shared data and must be run by only one thread at a time. It is protected by a lock and kept as short as possible.
Japanese name: クリティカルセクション
Even a simple statement like count++ compiles to a read, a modify and a write. If two threads run it at the same time, both may read the same old value and one increment is lost. Code that reads and updates shared data in this way is a critical section, and the bug that occurs when two threads enter it together is a data race or race condition. A correct solution must guarantee mutual exclusion, so only one thread is inside at a time, while still letting waiting threads make progress rather than wait forever.
The usual tool is a lock: a thread acquires a mutex before entering and releases it on exit, and any other thread trying to acquire it blocks or spins until it is free. Locks are built from atomic hardware instructions such as test-and-set, compare-and-swap, or load-linked/store-conditional, which read and update a memory word without interference. A spinlock busy-waits and suits very short sections on multicore systems, while a mutex that puts the waiting thread to sleep suits longer waits. Acquiring several locks in inconsistent orders can cause deadlock.
On a single-core microcontroller, the competing code is often an interrupt handler rather than another thread, so a critical section is commonly protected by disabling interrupts and re-enabling them afterward. Interrupts that arrive meanwhile are normally held pending and serviced on re-enable, but long masked periods delay timer and communication handling and can overflow buffers. The section must therefore be as short as possible. On a multicore system disabling interrupts on one core is not enough, because another core can still access the data, so a spinlock or atomic operation is also needed.
While one thread holds a lock, others wait, so long critical sections serialize a program and erase the benefit of extra cores. Designers shorten them, split one coarse lock into finer locks for independent data, or use lock-free atomic operations. Hardware transactional memory offers another route: the critical section runs speculatively as a transaction whose writes stay invisible to other threads. If no other thread touched the same data it commits all at once; on conflict it aborts, rolls back and retries or falls back to a normal lock.
Answer: A. A part of a program that operates on shared data and must be executed by only one thread at a time
If several threads simultaneously execute code that reads and updates a shared variable, a data race occurs. Such a part, which only one thread should run at a time, is called a critical section and is protected by taking a lock before entering and releasing it on exit. The longer the critical section, the longer other threads wait, so it should be kept as short as possible.
A critical section is the region of code that accesses shared data and must not run concurrently. A mutex is one mechanism for enforcing that, by letting only the thread holding it enter the region.
Other threads must wait while one is inside, and interrupts may be disabled during it on microcontrollers. Long sections reduce parallelism and delay response to other events.
On a single-core system where the only competitor is an interrupt handler, it usually is. On a multicore system other cores can still run, so a lock or atomic operation is required as well.
Critical section appears in 2 practice questions across 1 topic, including Multicore & Synchronization.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.