std::atomic<T>::is_lock_free

From cppreference.com
< cpp‎ | atomic‎ | atomic
 
 
Concurrency support library
Threads
(C++11)
(C++20)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Free functions for atomic operations
Free functions for atomic flags
Memory ordering
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
 
 
(since C++11)
bool is_lock_free() const noexcept;
bool is_lock_free() const volatile noexcept;

Checks whether the atomic operations on all objects of this type are lock-free.

Parameters

(none)

Return value

true if the atomic operations on the objects of this type are lock-free, false otherwise.

Notes

All atomic types except for std::atomic_flag may be implemented using mutexes or other locking operations, rather than using the lock-free atomic CPU instructions. Atomic types are also allowed to be sometimes lock-free, e.g. if only aligned memory accesses are naturally atomic on a given architecture, misaligned objects of the same type have to use locks.

The C++ standard recommends (but does not require) that lock-free atomic operations are also address-free, that is, suitable for communication between processes using shared memory.


Example

#include <iostream>
#include <utility>
#include <atomic>
 
struct A { int a[100]; };
struct B { int x, y; };
int main()
{
    std::cout << std::boolalpha
              << "std::atomic<A> is lock free? "
              << std::atomic<A>{}.is_lock_free() << '\n'
              << "std::atomic<B> is lock free? "
              << std::atomic<B>{}.is_lock_free() << '\n';
}

Possible output:

std::atomic<A> is lock free? false
std::atomic<B> is lock free? true

See also

checks if the atomic type's operations are lock-free
(function template)
specializes atomic operations for std::shared_ptr
(function template)
[static] (C++17)
indicates that the type is always lock-free
(public static member constant)