|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
atomic::is_lock_free | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
| Constants | ||||
(C++17) | ||||
| Specialized member functions | ||||
| Specialized for integral, floating-point(C++20) and pointer types | ||||
| Specialized for integral and pointer types only | ||||
(C++26) | ||||
(C++26) | ||||
| Specialized for integral types only | ||||
bool is_lock_free()constnoexcept; | (1) | (since C++11) |
bool is_lock_free()constvolatilenoexcept; | (2) | (since C++11) |
Checks whether the atomic operations on all objects of this type are lock-free.
Contents |
(none)
true if the atomic operations on the objects of this type are lock-free,false otherwise.
All atomic types except forstd::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 besometimes 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.
#include <atomic>#include <iostream>#include <utility> 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? falsestd::atomic<B> is lock free? true
(C++11) | checks if the atomic type's operations are lock-free (function template)[edit] |
| specializes atomic operations forstd::shared_ptr (function template) | |
[static](C++17) | indicates that the type is always lock-free (public static member constant)[edit] |