liveBook · Manning

Atomic_flag

As explained in C++ Concurrency in Action, Second Edition

A C++ standard library atomic Boolean flag with test-and-set and clear operations.

std::atomic_flag is a Boolean atomic that is guaranteed to be lock-free. It starts clear when initialized with ATOMIC_FLAG_INIT and cannot be copied or assigned. section 5.2.1 section 5.2.2

Its interface has just two state-changing operations:

  • clear() resets the flag. It is a store, so it cannot use memory_order_acquire or memory_order_acq_rel; its default order is memory_order_seq_cst. section 5.2.2
  • test_and_set() sets the flag and returns its previous value. It is a read-modify-write operation and accepts any memory-order tag; its default is memory_order_seq_cst. section 5.2.2
Listing 5.1. Implementation of a spinlock mutex using std::atomic_flag
class spinlock_mutex
{
    std::atomic_flag flag;
public:
    spinlock_mutex():
        flag(ATOMIC_FLAG_INIT)
    {}
    void lock()
    {
        while(flag.test_and_set(std::memory_order_acquire));
    }
    void unlock()
    {
        flag.clear(std::memory_order_release);
    }
};

A spinlock uses test_and_set() with acquire ordering to keep waiting while the flag is set, then clear() with release ordering to unlock. section 5.2.2

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Defines initialization and operations, explains memory-order constraints, and demonstrates a spinlock; broader atomic types are only contrasted.
Examples
Uses ATOMIC_FLAG_INIT, clear() and test_and_set(), then gives a spinlock mutex implementation.
What you can do after
Build a basic spinlock mutex by looping on acquire test_and_set() and unlocking with release clear().
How it connects
Connects atomic operations and memory ordering to spinlocks and mutexes.

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 C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.