liveBook · Manning

Atomic_flag

As explained in Modern C

atomic_flag is a C atomic type that supports indivisible flag operations for concurrency and signal-related contexts.

atomic_flag is a C atomic type whose flag operations appear indivisible in signal-handler contexts. This makes it suitable for passing limited state from an asynchronous signal handler to the interrupted program. section 17.7 section 17.6

Use atomic_flag within the narrow boundary imposed by asynchronous signals:

  • Keep the handler minimal: mark the interrupt condition, return to the interrupted context, and handle the condition there. section 17.7 section 17.6
  • Treat an uninterruptible operation as one observed either before it starts or after it completes; the implementation may still divide it internally. section 17.6
  • Distinguish atomic_flag from sig_atomic_t: sig_atomic_t reliably supports only indivisible evaluation and assignment, so it is unsuitable for counters. section 17.6
  • Remember that atomic_flag belongs to the C11 atomic facilities and requires stdatomic.h when atomics are available; __STDC_NO_ATOMICS__ signals their absence. section 17.6

Signal-handler boundary

A minimal handler updates signal-related state, handles immediate termination cases, or stores the interrupt value before returning. The interrupted context then performs the substantial work. atomic_flag is one permitted type for communicating that limited state; the surrounding example also conditionally counts handler calls with a lock-free atomic type. section 17.7

How Modern C covers it

How deep it goes
Defines observable indivisibility and availability, but does not develop atomic_flag syntax or operation details.
Examples
Uses a minimal signal handler that records interrupt state and returns, alongside conditional lock-free signal-call counting.
What it assumes
Assumes basic C signal-handler concepts while distinguishing sig_atomic_t and atomic types in context.
What you can do after
Apply it to communicate a small signal condition without making the handler perform uncontrolled program-state changes.
How it connects
Links atomic_flag with signal handlers, sig_atomic_t, lock-free atomic types, and asynchronous signal safety.

Also explained in

Compare how 2 books explain atomic_flag →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.