CalculateeLearning paths
Embedded Systems8–10 min read

Interrupts and Shared Data

Learn when interrupts help, why handlers must stay short and why volatile does not make shared operations atomic.

Mental model

An interrupt is a doorbell: stop briefly to note that someone arrived, then do the longer conversation in the main program.

main loopISRmain resumeseventreturn
An interrupt pauses normal code, runs a short service routine, then returns to the paused instruction stream.
UNDERSTAND THE BEHAVIOR
01

An interrupt requests execution of a handler in response to an event such as a timer match or input edge. The processor preserves enough context to resume the interrupted code afterward. This reduces dependence on polling, but it does not eliminate latency. Higher-priority work, disabled interrupts and handler execution time all affect when an event is serviced.

02

A handler should capture the minimum information needed and return. Printing messages, waiting for serial output, delaying or performing a long calculation inside it can block other events. Store a flag, timestamp or sample, then let the main loop process it. If every event matters, a single flag may be insufficient because several arrivals can collapse into one true value.

03

Shared data introduces another issue. An interrupt can occur between reading a value and writing an updated value. Even reading a multi-byte counter may tear on a narrower processor. Volatile can be required for certain compiler-visible accesses, but it is not a lock, does not make increments indivisible, and is not a general C++ thread synchronization mechanism.

04

Use the platform's supported critical sections, atomic operations or ISR-safe queues. Keep protected regions short and preserve the previous interrupt state rather than blindly enabling interrupts on exit. Before using a C++ atomic in an ISR, verify that its implementation is suitable and lock-free where required. Platform documentation governs these low-level details.

Why it matters

Learn when interrupts help, why handlers must stay short and why volatile does not make shared operations atomic.

Core ideas

  • Capture events quickly; process them outside the ISR.
  • A shared flag can lose repeated events.
  • Volatile and atomicity solve different problems.
Key equationsCPU fraction in ISR ≈ event rate × handler time1000/s × 10 μs = 1%
EXAMPLE

Work through a practical example

1An encoder produces 1000 edges per second.

2A 10 μs handler consumes about 10 ms each second, before other overhead.

3The main loop obtains a consistent snapshot using the platform's supported mechanism.

4If the loop is slow, check counter overflow and whether the input can deliver edges faster than the handler can service.

Common mistake

Assuming volatile counter++ is atomic, or calling blocking libraries from a handler.

RememberCapture events quickly; process them outside the ISR.

Practice and check your understanding

Try each question before opening the worked answer.

1. A handler takes 20 μs at 5000 events/s. What fraction of CPU time is used?

Show answer and reasoning

5000 × 20 μs = 0.1 s per second, approximately 10%, excluding additional overhead.

2. Can a boolean flag record exactly how many events arrived?

Show answer and reasoning

No. Multiple events before the flag is cleared collapse into one indication. Use a counter or queue with an appropriate concurrency mechanism.

Continue learning

Technical references and further reading

References are for further study, not endorsements. Report an error or suggest an improvement.

Progress stays on this browser only. Clearing site data removes it; it does not sync across devices.