Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/shared ptr/atomic"

From cppreference.com
< cpp‎ | memory‎ | shared ptr
m (Use since= and until= params of {{dcl}} template.)
m (s/partial specializations/overloads)
Line 67: Line 67:
 
{{dcl end}}
 
{{dcl end}}
  
If multiple threads of execution access the object referenced by the same {{lc|std::shared_ptr}} without synchronization, a data race may occur, unless all such access is performed through these functions, which are partial specializations of the corresponding atomic access functions ({{lc|std::atomic_load}}, {{lc|std::atomic_store}}, etc)
+
If multiple threads of execution access the object referenced by the same {{lc|std::shared_ptr}} without synchronization, a data race may occur, unless all such access is performed through these functions, which are overloads of the corresponding atomic access functions ({{lc|std::atomic_load}}, {{lc|std::atomic_store}}, etc)
  
 
Note that the control block of a shared_ptr is thread-safe: different {{lc|std::shared_ptr}} objects can be accessed using mutable operations, such as operator= or reset, simultaneously by multiple threads, even when these instances are copies, and share the same control block internally.
 
Note that the control block of a shared_ptr is thread-safe: different {{lc|std::shared_ptr}} objects can be accessed using mutable operations, such as operator= or reset, simultaneously by multiple threads, even when these instances are copies, and share the same control block internally.

Revision as of 11:09, 23 September 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 
template< class T >
bool atomic_is_lock_free( const std::shared_ptr<T>* p );
(1) (since C++11)
template< class T >
std::shared_ptr<T> atomic_load( const std::shared_ptr<T>* p );
(2) (since C++11)
template< class T >

std::shared_ptr<T> atomic_load_explicit( const shared_ptr<T>* p,

                                         std::memory_order mo );
(3) (since C++11)
template< class T >

void atomic_store( std::shared_ptr<T>* p,

                   std::shared_ptr<T> r );
(4) (since C++11)
template< class T >

void atomic_store_explicit( std::shared_ptr<T>* p,
                            shared_ptr<T> r,

                            std::memory_order mo);
(5) (since C++11)
template< class T >

std::shared_ptr<T> atomic_exchange( std::shared_ptr<T>* p,

                                    std::shared_ptr<T> r);
(6) (since C++11)
template<class T>

std::shared_ptr<T> atomic_exchange_explicit( std::shared_ptr<T>* p,
                                             std::shared_ptr<T> r,

                                             std::memory_order mo);
(7) (since C++11)
template< class T >

bool atomic_compare_exchange_weak( std::shared_ptr<T>* p,
                                   std::shared_ptr<T>* expected,

                                   std::shared_ptr<T> desired);
(8) (since C++11)
template<class T>

bool atomic_compare_exchange_strong( std::shared_ptr<T>* p,
                                     std::shared_ptr<T>* expected,

                                     std::shared_ptr<T> desired);
(9) (since C++11)
template< class T >

bool atomic_compare_exchange_strong_explicit( std::shared_ptr<T>* p,
                                              std::shared_ptr<T>* expected,
                                              std::shared_ptr<T> desired,
                                              std::memory_order success,

                                              std::memory_order failure);
(10) (since C++11)
template< class T >

bool atomic_compare_exchange_weak_explicit( std::shared_ptr<T>* p,
                                            std::shared_ptr<T>* expected,
                                            std::shared_ptr<T> desired,
                                            std::memory_order success,

                                            std::memory_order failure);
(11) (since C++11)

If multiple threads of execution access the object referenced by the same std::shared_ptr without synchronization, a data race may occur, unless all such access is performed through these functions, which are overloads of the corresponding atomic access functions (std::atomic_load, std::atomic_store, etc)

Note that the control block of a shared_ptr is thread-safe: different std::shared_ptr objects can be accessed using mutable operations, such as operator= or reset, simultaneously by multiple threads, even when these instances are copies, and share the same control block internally.

1) Determines whether atomic access to the shared pointer pointed-to by p is lock-free.
2) Equivalent to atomic_load_explicit(p, std::memory_order_seq_cst)
3) Returns the shared pointer pointed-to by p. As with the non-specialized std::atomic_load_explicit, mo cannot be std::memory_order_release or std::memory_order_acq_rel
4) Equivalent to atomic_store_explicit(p, r, memory_order_seq_cst)
5) Swaps the shared pointers p and r, effectively executing p->swap(r). As with the non-specialized std::atomic_store_explicit, mo cannot be std::memory_order_acquire or std::memory_order_acq_rel
6) Equivalent to atomic_exchange_explicit(p, r, memory_order_seq_cst)
7) Swaps the shared pointers p and r, effectively executing p->swap(r) and returns a copy of the shared pointer formerly pointed-to by p
8) Equivalent to atomic_compare_exchange_weak_explicit(p, expected, desired, std::memory_order_seq_cst, std::memory_order_seq_cst)
9) Equivalent to atomic_compare_exchange_strong_explicit(p, expected, desired, std::memory_order_seq_cst, std::memory_order_seq_cst)
10) Compares the shared pointers pointed-to by p and expected. If they are equivalent (share ownership of the same pointer and refer to the same pointer), assigns desired into *p using the memory ordering constraints specified by success and returns true. If they are not equivalent, assigns *p into *expected using the memory ordering constraints specified by failure and returns false.
11) Same as 10), but may fail spuriously.

All these functions invoke undefined behavior if p is a null pointer.

Contents

Parameters

p, expected - a pointer to a std::shared_ptr
r, desired - a std::shared_ptr
mo, success, failure - memory ordering selectors of type std::memory_order

Exceptions

These functions do not throw exceptions.

Return value

1) true if atomic access is implemented using lock-free instructions
2,3) A copy of the pointed-to shared pointer.
4,5) (none)
6,7) A copy of the formerly pointed-to shared pointer
8,9,10,11) true if the shared pointers were equivalent and the exchange was performed, false otherwise.

Example

See also

checks if the atomic type's operations are lock-free
(function template) [edit]
atomically replaces the value of the atomic object with a non-atomic argument
(function template) [edit]
atomically obtains the value stored in an atomic object
(function template) [edit]
atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic
(function template) [edit]
atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not
(function template) [edit]