Difference between revisions of "cpp/atomic/atomic is lock free"
(LWG 3249) |
|||
Line 30: | Line 30: | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Determines if the atomic object pointed to by {{tt|obj}} is implemented lock-free, as if by calling {{c|obj->is_lock_free()}}. In any given program execution, the result of the lock-free query is the same for all | + | @1@ Determines if the atomic object pointed to by {{tt|obj}} is implemented lock-free, as if by calling {{c|obj->is_lock_free()}}. In any given program execution, the result of the lock-free query is the same for all atomic objects of the same type.{{mark unreviewed dr|LWG}}<!-- LWG 1146 & N2992 --> |
@2,3@ Expands to an integer constant expression with value | @2,3@ Expands to an integer constant expression with value | ||
Line 44: | Line 44: | ||
===Return value=== | ===Return value=== | ||
{{c|true}} if {{c|*obj}} is a lock-free atomic, {{c|false}} otherwise. | {{c|true}} if {{c|*obj}} is a lock-free atomic, {{c|false}} otherwise. | ||
− | |||
− | |||
===Notes=== | ===Notes=== | ||
Line 75: | Line 73: | ||
std::atomic<B> is lock free? true | std::atomic<B> is lock free? true | ||
}} | }} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=3249|std=C++11|before={{tt|atomic_is_lock_free}} was specified via pointers, which was<br>ambiguous and might accept invalid pointer values|after=specified via atomic objects}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/atomic/atomic/dsc is_lock_free | + | {{dsc inc | cpp/atomic/atomic/dsc is_lock_free}} |
{{dsc tfun | cpp/memory/shared_ptr/atomic | title=std::atomic_is_lock_free{{dsc small|(std::shared_ptr)}} | specializes atomic operations for {{lc|std::shared_ptr}} }} | {{dsc tfun | cpp/memory/shared_ptr/atomic | title=std::atomic_is_lock_free{{dsc small|(std::shared_ptr)}} | specializes atomic operations for {{lc|std::shared_ptr}} }} | ||
{{dsc inc | cpp/atomic/dsc atomic_flag}} | {{dsc inc | cpp/atomic/dsc atomic_flag}} | ||
Line 86: | Line 89: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 00:34, 25 November 2020
Defined in header <atomic>
|
||
(1) | (since C++11) | |
template< class T > bool atomic_is_lock_free( const volatile std::atomic<T>* obj ) noexcept; |
||
template< class T > bool atomic_is_lock_free( const std::atomic<T>* obj ) noexcept; |
||
#define ATOMIC_BOOL_LOCK_FREE /* unspecified */ #define ATOMIC_CHAR_LOCK_FREE /* unspecified */ |
(2) | (since C++11) |
#define ATOMIC_CHAR8_T_LOCK_FREE /* unspecified */ |
(3) | (since C++20) |
obj
is implemented lock-free, as if by calling obj->is_lock_free(). In any given program execution, the result of the lock-free query is the same for all atomic objects of the same type.- 0 for the built-in atomic types that are never lock-free
- 1 for the built-in atomic types that are sometimes lock-free
- 2 for the built-in atomic types that are always lock-free.
Contents |
Parameters
obj | - | pointer to the atomic object to examine |
Return value
true if *obj is a lock-free atomic, 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: for example, if only some subarchitectures support lock-free atomic access for a given type (such as the CMPXCHG16B instruction on x86-64), whether atomics are lock-free may not be known until runtime.
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::atomic<A> a; std::atomic<B> b; std::cout << std::boolalpha << "std::atomic<A> is lock free? " << std::atomic_is_lock_free(&a) << '\n' << "std::atomic<B> is lock free? " << std::atomic_is_lock_free(&b) << '\n'; }
Possible output:
std::atomic<A> is lock free? false std::atomic<B> is lock free? true
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3249 | C++11 | atomic_is_lock_free was specified via pointers, which wasambiguous and might accept invalid pointer values |
specified via atomic objects |
See also
checks if the atomic object is lock-free (public member function of std::atomic<T> )
| |
specializes atomic operations for std::shared_ptr (function template) | |
(C++11) |
the lock-free boolean atomic type (class) |
[static] (C++17) |
indicates that the type is always lock-free (public static member constant of std::atomic<T> )
|
C documentation for atomic_is_lock_free
| |
C documentation for ATOMIC_*_LOCK_FREE
|