skip to content

Why can a C++ volatile counter incremented by a UART interrupt handler and decremented by the main loop still lose updates?

level: middleimportance: must knowfreq 60%

answer

  1. load, add, store
  2. the interrupt lands in between
  3. a data race in the standard's terms
  4. lock-free atomic or masked interrupts

basics

~20 s

Incrementing a volatile counter is still a separate load, add and store. If the interrupt fires between the main loop's load and store, one update is overwritten. Use a lock-free std::atomic or mask the interrupt around the update.

solid answer

~50 s

`volatile` keeps every access in the generated code but does not make a read-modify-write indivisible. The main loop's `pending = pending - 1` is a load, a subtract and a store; Clang emits `ldr`, `adds`, `str` for the increment on a Cortex-M. If the receive interrupt runs between the main loop's load and its store, the handler's increment is overwritten and a byte is lost from the count. The standard calls this a data race: the handler and the code it interrupted are potentially concurrent, and an unsynchronized conflicting access to a non-atomic object is undefined behaviour; the one exception is `volatile std::sig_atomic_t`, and even that only makes single loads and stores safe. The fix is a `std::atomic` that is lock-free (`fetch_add`, `fetch_sub`) or a short critical section that masks the interrupt around the update. C++20 deprecated `++` and `--` on volatile objects partly because they look atomic and are not.

code

plaintext · 7 lines
plaintext
_Z11uart_rx_isrv:                       @ rx_count = rx_count + 1;  (volatile unsigned)
	movw	r0, :lower16:rx_count
	movt	r0, :upper16:rx_count
	ldr	r1, [r0]
	adds	r1, #1
	str	r1, [r0]
	bx	lr

go deeper

for a junior

Recall that volatile keeps each access but an increment is still a separate read and write.

for a middle

Walk through the interleaving that loses an update and name the fixes: a lock-free atomic or masking the interrupt.

for a senior

Explain the standard's signal-handler model, why the atomic must be lock-free, and when a single-writer redesign beats both fixes.

for a principal

Decide the project rule for state shared with interrupts and how review or static analysis catches volatile used as a lock.

## The scenario A UART driver counts bytes waiting in its receive buffer. The interrupt handler increments the count when a byte arrives; the main loop decrements it after consuming one: ```cpp volatile unsigned pending = 0; void uart_rx_isr() { pending = pending + 1; } // in the interrupt void consume() { pending = pending - 1; } // in the main loop ``` After running for hours the count drifts, and the main loop either stalls with bytes still in the buffer or reads stale slots. Everything is `volatile`, so the developer's first instinct is that the compiler cannot have broken it. It has not: the source itself is wrong. ## What the hardware actually executes `volatile` guarantees that each access in the source happens. It says nothing about grouping accesses. On a Cortex-M, Clang 21 at `-O2` compiles the handler's increment to three instructions: `ldr`, `adds`, `str` (shown in the code example). The main loop's decrement is the same shape. Now interleave them: 1. The main loop loads `pending` (value 3) into a register. 2. A byte arrives; the interrupt handler loads 3, adds 1 and stores 4. 3. The main loop resumes, subtracts 1 from its stale 3 and stores 2. Two events happened, one up and one down, so the right answer is 3; the count says 2. One byte is now invisible to the consumer. ## The standard's view C++ has no notion of hardware interrupts, but its model of a **signal handler** is the closest match, and embedded compilers treat interrupt handlers the same way. Under that model: - An action in a signal handler and an unsequenced action in the code it interrupted are **potentially concurrent**. - Two potentially concurrent conflicting accesses, at least one of them non-atomic and neither happening before the other, form a **data race**, which is undefined behaviour. - The single exception is an object of type `volatile std::sig_atomic_t`: plain loads and stores of it by the handler and the interrupted thread are not a race. A read-modify-write of it is still two accesses and can still lose an update. So a `volatile unsigned` read-modify-write shared with a handler is a data race in the standard's terms, not merely a timing problem. ## Fixes that work | Fix | How | Cost | |---|---|---| | Lock-free atomic | `std::atomic<unsigned> pending;` with `pending.fetch_add(1)` in the handler and `pending.fetch_sub(1)` in the loop | needs atomic instructions (on Armv7-M, exclusive load/store) | | Mask the interrupt | disable the UART interrupt, update, re-enable, in the main loop only | interrupt latency for the masked window | | Redesign | two counters each written by one side, compare them | no read-modify-write is shared | Points to check before choosing: - The atomic must be **lock-free**. A `std::atomic` that falls back to a lock can deadlock when the interrupt handler waits for a lock held by the code it interrupted. Check with `static_assert(std::atomic<unsigned>::is_always_lock_free);` (C++17). - On cores without atomic read-modify-write instructions, such as Armv6-M, `fetch_add` cannot be one atomic instruction, so the toolchain has to supply it some other way; check how before calling it from a handler, or mask interrupts instead. - The redesign is often the best answer: a ring buffer whose head index only the handler writes and whose tail index only the main loop writes needs no read-modify-write of shared data. Ordering those indices against the buffer is the next problem, and `std::atomic` with release and acquire handles it. ## Where else the same bug hides - **Wide values on a narrow core.** A 64-bit tick counter on a 32-bit core is written with two stores, so even a plain read can see half of an update; a `volatile` qualifier changes nothing. - **Flag words.** Setting one bit with `flags = flags | RX_READY` in the handler while the main loop clears another bit the same way is the identical lost update on a different variable. - **Statistics counters.** Error and overrun counters are usually incremented in the handler and reset in the main loop, and the reset can wipe out an increment. ## The language's own hint C++20 deprecated `++` and `--` on volatile-qualified objects, and Clang warns `increment of object of volatile-qualified type ... is deprecated` from `-std=c++20` on. One motivation was exactly this bug: the operator reads like a single indivisible step, and on a volatile object it never was. Writing `pending = pending + 1` makes the two accesses visible, which is honest but still racy; the deprecation is a hint to reach for an atomic.

  • Why must a std::atomic shared with an interrupt handler be lock-free?
    If the atomic falls back to an internal lock, the main loop may hold that lock when the interrupt fires. The handler then waits for a lock that can only be released by the code it interrupted, which never runs again: a deadlock. `static_assert(std::atomic<T>::is_always_lock_free)` turns that risk into a compile error.
  • Is a volatile std::sig_atomic_t flag enough for the counter?
    No. The standard makes single loads and stores of a `volatile std::sig_atomic_t` race-free between a handler and the thread it interrupts, so it suits a one-way flag. An increment is still a load and a store with a gap between them, so concurrent updates can still be lost.

  • volatile makes ++ on a shared counter atomic.
  • A data race needs two threads; an interrupt cannot cause one.
  • Any std::atomic is safe to use inside an interrupt handler.
  • volatile std::sig_atomic_t makes increments safe from a handler.
  • The lost update is a compiler bug, since the variable is volatile.