Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/atomic/atomic is lock free"

From cppreference.com
< cpp‎ | atomic
m (Text replace - "{{noexcept" to "{{unreviewed noexcept")
(Uses {{mark life}}.)
 
(11 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{title|{{dsc small|std::}}atomic_is_lock_free, ATOMIC_xxx_LOCK_FREE}}
+
{{cpp/title|atomic_is_lock_free, ATOMIC_xxx_LOCK_FREE}}
{{cpp/atomic/navbar}}
+
{{cpp/thread/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | atomic }}
+
{{dcl header|atomic}}
{{dcl rev begin | num=1 | since=c++11}}
+
{{dcl|num=1|since=c++11|
{{dcl | 1=
+
 
template< class T >
 
template< class T >
bool atomic_is_lock_free( const volatile std::atomic<T>* obj );
+
bool atomic_is_lock_free( const volatile std::atomic<T>* obj ) noexcept;
 
}}
 
}}
{{dcl | 1=
+
{{dcl|num=2|since=c++11|
 
template< class T >
 
template< class T >
bool atomic_is_lock_free( const std::atomic<T>* obj );
+
bool atomic_is_lock_free( const std::atomic<T>* obj ) noexcept;
 
}}
 
}}
{{dcl rev end}}
+
{{dcl|num=3|since=c++11|
{{dcl | num=2 | since=c++11 | 1=
+
 
#define ATOMIC_BOOL_LOCK_FREE    /* unspecified */
 
#define ATOMIC_BOOL_LOCK_FREE    /* unspecified */
 
#define ATOMIC_CHAR_LOCK_FREE    /* unspecified */
 
#define ATOMIC_CHAR_LOCK_FREE    /* unspecified */
Line 24: Line 22:
 
#define ATOMIC_LLONG_LOCK_FREE    /* unspecified */
 
#define ATOMIC_LLONG_LOCK_FREE    /* unspecified */
 
#define ATOMIC_POINTER_LOCK_FREE  /* unspecified */
 
#define ATOMIC_POINTER_LOCK_FREE  /* unspecified */
 +
}}
 +
{{dcl|num=4|since=c++20|
 +
#define ATOMIC_CHAR8_T_LOCK_FREE  /* unspecified */
 
}}
 
}}
 
{{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 pointers of the same type.<!-- LWG 1146 & N2992 -->
+
@1,2@ Determines if the atomic object pointed to by {{c|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.
  
@2@ Expands to an integer constant expression with value
+
@3,4@ Expands to an integer constant expression with value
* {{c|0}} for the built-in atomic types that are never lock-free
+
* {{c|0}} for the built-in atomic types that are never lock-free,
* {{c|1}} for the built-in atomic types that are ''sometimes'' lock-free
+
* {{c|1}} for the built-in atomic types that are ''sometimes'' lock-free,
 
* {{c|2}} for the built-in atomic types that are always lock-free.
 
* {{c|2}} for the built-in atomic types that are always lock-free.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | obj | pointer to the atomic object to examine}}
+
{{par|obj|pointer to the atomic object to examine}}
 
{{par end}}  
 
{{par end}}  
  
 
===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.
 
===Exceptions===
 
{{unreviewed noexcept}}
 
  
 
===Notes===
 
===Notes===
All atomic types except for {{lc|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.
+
All atomic types except for {{lc|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.
 
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.
Line 52: Line 50:
 
===Example===
 
===Example===
 
{{example|code=
 
{{example|code=
 +
#include <atomic>
 
#include <iostream>
 
#include <iostream>
 
#include <utility>
 
#include <utility>
#include <atomic>
 
  
 
struct A { int a[100]; };
 
struct A { int a[100]; };
 
struct B { int x, y; };
 
struct B { int x, y; };
 +
 
int main()
 
int main()
 
{
 
{
Line 73: Line 72:
 
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<br>was ambiguous and might accept invalid pointer values|after=specified via<br>atomic objects}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/atomic/atomic/dsc is_lock_free | mem=std::atomic<T>}}
+
{{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 inc|cpp/atomic/dsc atomic_flag}}
{{dsc inc | cpp/atomic/dsc atomic_flag}}
+
{{dsc inc|cpp/atomic/atomic/dsc is_always_lock_free}}
{{dsc inc | cpp/atomic/atomic/dsc is_always_lock_free}}
+
{{dsc break}}
{{dsc see c | c/atomic/atomic_is_lock_free}}
+
{{dsc tfun|cpp/memory/shared_ptr/atomic|notes={{mark life|deprecated=c++20|removed=c++26|br=yes}}|title=std::atomic_is_lock_free{{dsc small|(std::shared_ptr)}}|specializes atomic operations for {{lc|std::shared_ptr}}}}
{{dsc see c | c/atomic/ATOMIC_LOCK_FREE_consts | ATOMIC_*_LOCK_FREE}}
+
{{dsc see c|c/atomic/atomic_is_lock_free}}
 +
{{dsc see c|c/atomic/ATOMIC_LOCK_FREE_consts|ATOMIC_*_LOCK_FREE}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/atomic/atomic is lock free]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/atomic/atomic is lock free]]
+
[[fr:cpp/atomic/atomic is lock free]]
+
[[it:cpp/atomic/atomic is lock free]]
+
[[ja:cpp/atomic/atomic is lock free]]
+
[[pt:cpp/atomic/atomic is lock free]]
+
[[ru:cpp/atomic/atomic is lock free]]
+
[[zh:cpp/atomic/atomic is lock free]]
+

Latest revision as of 04:44, 24 April 2024

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(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)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
atomic_is_lock_free
(C++11)
Free functions for atomic flags
 
Defined in header <atomic>
template< class T >
bool atomic_is_lock_free( const volatile std::atomic<T>* obj ) noexcept;
(1) (since C++11)
template< class T >
bool atomic_is_lock_free( const std::atomic<T>* obj ) noexcept;
(2) (since C++11)
#define ATOMIC_BOOL_LOCK_FREE     /* unspecified */

#define ATOMIC_CHAR_LOCK_FREE     /* unspecified */
#define ATOMIC_CHAR16_T_LOCK_FREE /* unspecified */
#define ATOMIC_CHAR32_T_LOCK_FREE /* unspecified */
#define ATOMIC_WCHAR_T_LOCK_FREE  /* unspecified */
#define ATOMIC_SHORT_LOCK_FREE    /* unspecified */
#define ATOMIC_INT_LOCK_FREE      /* unspecified */
#define ATOMIC_LONG_LOCK_FREE     /* unspecified */
#define ATOMIC_LLONG_LOCK_FREE    /* unspecified */

#define ATOMIC_POINTER_LOCK_FREE  /* unspecified */
(3) (since C++11)
#define ATOMIC_CHAR8_T_LOCK_FREE  /* unspecified */
(4) (since C++20)
1,2) Determines if the atomic object pointed to by 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.
3,4) Expands to an integer constant expression with value
  • 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

[edit] Parameters

obj - pointer to the atomic object to examine

[edit] Return value

true if *obj is a lock-free atomic, false otherwise.

[edit] 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.

[edit] Example

#include <atomic>
#include <iostream>
#include <utility>
 
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

[edit] 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
was ambiguous and might accept invalid pointer values
specified via
atomic objects

[edit] See also

checks if the atomic object is lock-free
(public member function of std::atomic<T>) [edit]
the lock-free boolean atomic type
(class) [edit]
[static] (C++17)
indicates that the type is always lock-free
(public static member constant of std::atomic<T>) [edit]
(deprecated in C++20)(removed in C++26)
specializes atomic operations for std::shared_ptr
(function template)
C documentation for atomic_is_lock_free
C documentation for ATOMIC_*_LOCK_FREE