skip to content

In C++, what do is_always_lock_free and is_lock_free tell you about a std::atomic<T>, and what happens when they are false?

level: middleimportance: nice to knowfreq 30%

answer

  1. trivially copyable T
  2. compile-time constant vs runtime answer
  3. a hidden lock, still atomic
  4. size and target decide
  5. atomic_ref and atomic shared_ptr

basics

~20 s

They report whether operations on that atomic type avoid locks: is_always_lock_free at compile time, is_lock_free at run time. When false, the type is still fully atomic, but the library may use a lock, so operations can block.

solid answer

~40 s

`std::atomic<T>` accepts any trivially copyable `T`, but only some sizes map to hardware atomic instructions. `std::atomic<T>::is_always_lock_free` (C++17) is a `static constexpr bool` you can `static_assert`; `is_lock_free()` answers at run time and is the same for every object of that type in one execution. When either is false the operations remain atomic and correctly ordered — the library just protects them with a lock, and the standard says such operations are considered to potentially block, which matters in signal handlers, interrupt handlers and real-time threads. With Apple clang and libc++ on arm64, a 16-byte struct reported lock-free and a 24-byte one did not; the answer depends on target, compiler and flags. `std::atomic<std::shared_ptr<T>>` (C++20) is a typical non-lock-free case: libstdc++ sets its `is_always_lock_free` to `false`.

code

cpp · 15 lines
cpp
#include <atomic>
#include <cstdio>

struct Pair { long a, b; };       // 16 bytes on a 64-bit target
struct Triple { long a, b, c; };  // 24 bytes

int main() {
    std::atomic<Triple> t{Triple{1, 2, 3}};
    std::printf("int    %d\n", std::atomic<int>::is_always_lock_free);
    std::printf("Pair   %d\n", std::atomic<Pair>::is_always_lock_free);
    std::printf("Triple %d (runtime: %d)\n",
                std::atomic<Triple>::is_always_lock_free, t.is_lock_free());
    Triple old = t.exchange(Triple{4, 5, 6});  // still atomic, via a lock
    std::printf("old.c=%ld new.c=%ld\n", old.c, t.load().c);
}

go deeper

for a junior

Recall that std::atomic<T> needs a trivially copyable T and that is_lock_free says whether a hidden lock is used.

for a middle

Distinguish the compile-time is_always_lock_free from the run-time is_lock_free, and explain that false still means atomic but potentially blocking.

for a senior

Static-assert lock-freedom where a signal handler or real-time thread depends on it, and know that atomic<shared_ptr> is a lock in libstdc++ and missing in libc++.

for a principal

Decide per target which atomic types the codebase may assume lock-free, and encode that in static assertions rather than reviewer memory.

## Which `T` can be atomic The primary template `std::atomic<T>` accepts any **trivially copyable** type; the program is ill-formed otherwise. That allows structs such as a `{position, velocity}` pair to be loaded, stored and compare-exchanged as one unit. Whether the hardware can do that in one instruction depends on the type's size and alignment and the target, which is what the lock-free queries report. ## The two queries | Query | Kind | Since | Answers | |---|---|---|---| | `std::atomic<T>::is_always_lock_free` | `static constexpr bool` | C++17 | lock-free for every object of the type, known at compile time | | `a.is_lock_free()` | member function | C++11 | lock-free for objects of this type in this execution | | `ATOMIC_INT_LOCK_FREE` and friends | macros, 0, 1 or 2 | C++11 | 2 = always, 1 = sometimes, 0 = never | The standard adds that the run-time answer is the same for all atomic objects of the same type in a given execution, and that on a hosted implementation at least one signed integral specialisation and its unsigned counterpart are always lock-free. In practice the integral types up to the machine word are lock-free on every mainstream target, but the standard does not single out `int`. ## What "false" means A non-lock-free atomic is **still atomic**. Every operation is indivisible and obeys its memory order; the implementation simply guards the object with a lock. How that lock is provided — stored beside the value, or taken from a table of locks in a runtime library — is the implementation's choice, not the standard's. Consequences: - The standard says atomic operations that are not lock-free are considered to **potentially block**. - A signal handler may not safely use it: the interrupted thread could hold the lock. - A real-time audio or interrupt path loses its no-block guarantee. - Cost per operation rises to a lock acquisition, and with a lock-table design unrelated objects can contend on the same lock. ## What a real compiler reports On arm64 with Apple clang and libc++, the example below prints `1` for `int` and for a 16-byte `Pair`, and `0` for a 24-byte `Triple`, whose `exchange` still works atomically. On x86-64 the 16-byte answer can differ by compiler and flags, so never assume it: write `static_assert(std::atomic<Pair>::is_always_lock_free);` where lock-freedom is a requirement, and let the build fail on a target that cannot provide it. ## `std::atomic_ref` (C++20) `std::atomic_ref<T>` applies atomic operations to an object that is **not** declared atomic — an element of a large array used atomically only in one phase. Its rules: 1. The referenced object must be aligned to `std::atomic_ref<T>::required_alignment`. 2. While any `atomic_ref` to the object exists, every access must go through an `atomic_ref`. 3. It has its own `is_always_lock_free` and `is_lock_free()`, and the standard notes that a non-lock-free `atomic_ref` may acquire a lock associated with the object. ## `std::atomic<std::shared_ptr<T>>` (C++20) C++20 added the partial specialisations `std::atomic<std::shared_ptr<T>>` and `std::atomic<std::weak_ptr<T>>`, replacing the free functions `std::atomic_load(const std::shared_ptr<T>*)` and friends, which C++20 deprecated and C++26 removes. Implementation status differs: - libstdc++ (GCC 16.2 headers) implements it with `is_always_lock_free = false`, using the low bit of the control-block pointer as a lock. - libc++ (LLVM 23) does not implement it yet: `std::atomic<std::shared_ptr<int>>` falls to the primary template and fails its `is_trivially_copyable` static assertion. So the type is portable C++20 on paper, and a lock in practice where it exists. ## Using the answers in practice - Where blocking is merely slower, ignore the flags: correctness does not change, and a profiler will show whether the lock matters. - Where blocking is forbidden — a signal handler, an interrupt path, a real-time thread — require `is_always_lock_free` at compile time and pick a type small enough to satisfy it, such as an index instead of a struct. - Where a large value must change atomically and lock-freedom is required, publish a pointer to an immutable copy instead of making the large value itself atomic; the ownership of the old copy then becomes the hard part.

  • How do you make the build fail if an atomic you rely on in a signal handler is not lock-free?
    `static_assert(std::atomic<T>::is_always_lock_free);` next to the declaration. It is a compile-time constant since C++17, so a port to a target where the type needs a lock stops at compile time instead of deadlocking in a handler. `std::atomic_flag` is the one type whose operations the standard requires to be lock-free on every implementation.
  • Why does std::atomic<std::shared_ptr<int>> fail to compile with libc++?
    libc++ has not implemented the C++20 partial specialisation (its status page leaves the paper open), so the name selects the primary `std::atomic<T>` template, whose static assertion requires a trivially copyable `T`; `std::shared_ptr` is not. Until it lands, protect a shared `std::shared_ptr` with a mutex or use a library that provides an atomic shared pointer.

  • If is_lock_free() is false, operations on the atomic are not atomic.
  • std::atomic<T> works for any T, including std::string.
  • std::atomic<int> is guaranteed lock-free by the standard on every implementation.
  • is_lock_free() can differ between two objects of the same type in one run.
  • atomic<shared_ptr> is lock-free, so it beats a mutex-protected shared_ptr.

Assumes C++23; is_always_lock_free is C++17, atomic_ref and atomic<shared_ptr> are C++20, and C++26 removes the old shared_ptr atomic free functions.