Difference between revisions of "cpp/atomic/atomic compare exchange"
(fmt) |
(fmt) |
||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header | atomic }} | {{dcl header | atomic }} | ||
− | {{dcl rev begin | num=1 | + | {{dcl rev begin | num=1 | since=c++11}} |
− | + | {{dcl | | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_weak( std::atomic<T>* obj, | bool atomic_compare_exchange_weak( std::atomic<T>* obj, | ||
T* expected, T desired ); | T* expected, T desired ); | ||
}} | }} | ||
− | {{dcl | + | {{dcl | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj, | bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj, | ||
Line 15: | Line 15: | ||
}} | }} | ||
{{dcl rev end}} | {{dcl rev end}} | ||
− | {{dcl rev begin | num=2 | + | {{dcl rev begin | num=2 | since=c++11}} |
− | + | {{dcl | | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_strong( std::atomic<T>* obj, | bool atomic_compare_exchange_strong( std::atomic<T>* obj, | ||
T* expected, T desired ); | T* expected, T desired ); | ||
}} | }} | ||
− | {{dcl | + | {{dcl | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_strong( volatile std::atomic<T>* obj, | bool atomic_compare_exchange_strong( volatile std::atomic<T>* obj, | ||
Line 27: | Line 27: | ||
}} | }} | ||
{{dcl rev end}} | {{dcl rev end}} | ||
− | {{dcl rev begin | num=3 | + | {{dcl rev begin | num=3 | since=c++11}} |
− | + | {{dcl | | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_weak_explicit( std::atomic<T>* obj, | bool atomic_compare_exchange_weak_explicit( std::atomic<T>* obj, | ||
Line 35: | Line 35: | ||
std::memory_order fail ); | std::memory_order fail ); | ||
}} | }} | ||
− | {{dcl | + | {{dcl | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_weak_explicit( volatile std::atomic<T>* obj, | bool atomic_compare_exchange_weak_explicit( volatile std::atomic<T>* obj, | ||
Line 43: | Line 43: | ||
}} | }} | ||
{{dcl rev end}} | {{dcl rev end}} | ||
− | {{dcl rev begin | num=4 | + | {{dcl rev begin | num=4 | since=c++11}} |
− | + | {{dcl | | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_strong_explicit( std::atomic<T>* obj, | bool atomic_compare_exchange_strong_explicit( std::atomic<T>* obj, | ||
Line 51: | Line 51: | ||
std::memory_order fail ); | std::memory_order fail ); | ||
}} | }} | ||
− | {{dcl | + | {{dcl | |
template< class T > | template< class T > | ||
bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj, | bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj, | ||
Line 93: | Line 93: | ||
|code= | |code= | ||
#include <atomic> | #include <atomic> | ||
− | template< | + | |
+ | template<class T> | ||
struct node | struct node | ||
{ | { | ||
Line 101: | Line 102: | ||
}; | }; | ||
− | template< | + | template<class T> |
class stack | class stack | ||
{ | { | ||
Line 126: | Line 127: | ||
} | } | ||
}; | }; | ||
+ | |||
int main() | int main() | ||
{ | { |
Revision as of 10:48, 23 September 2013
Defined in header <atomic>
|
||
(1) | (since C++11) | |
template< class T > bool atomic_compare_exchange_weak( std::atomic<T>* obj, |
||
template< class T > bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj, |
||
(2) | (since C++11) | |
template< class T > bool atomic_compare_exchange_strong( std::atomic<T>* obj, |
||
template< class T > bool atomic_compare_exchange_strong( volatile std::atomic<T>* obj, |
||
(3) | (since C++11) | |
template< class T > bool atomic_compare_exchange_weak_explicit( std::atomic<T>* obj, |
||
template< class T > bool atomic_compare_exchange_weak_explicit( volatile std::atomic<T>* obj, |
||
(4) | (since C++11) | |
template< class T > bool atomic_compare_exchange_strong_explicit( std::atomic<T>* obj, |
||
template< class T > bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj, |
||
Atomically compares the value pointed to by obj
with the value pointed to by expected
, and if those are equal, replaces the former with desired
(performs read-modify-write operation). Otherwise, loads the actual value pointed to by obj
into *expected
(performs load operation).
The memory models for the read-modify-write and load operations are succ
and fail
respectively. The (1-2) versions use std::memory_order_seq_cst by default.
The weak forms ((1) and (3)) of the functions are allowed to fail spuriously, that is, act as if *obj != *expected even if they are equal. When a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms. When a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable.
These functions are defined in terms of member functions of std::atomic:
Contents |
Parameters
obj | - | pointer to the atomic object to test and modify |
expected | - | pointer to the value expected to be found in the atomic object |
desired | - | the value to store in the atomic object if it is as expected |
succ | - | the memory sycnhronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted. |
fail | - | the memory sycnhronization ordering for the load operation if the comparison fails. Cannot be std::memory_order_release or std::memory_order_acq_rel and cannot specify stronger ordering than succ
|
Return value
The result of the comparison: true if *obj
was equal to *exp
, false otherwise.
Exceptions
Example
compare and exchange operations are often used as basic building blocks of lockfree data structures
#include <atomic> template<class T> struct node { T data; node* next; node(const T& data) : data(data), next(nullptr) {} }; template<class T> class stack { std::atomic<node<T>*> head; public: void push(const T& data) { node<T>* new_node = new node<T>(data); // put the current value of head into new_node->next new_node->next = head.load(std::memory_order_relaxed); // now make new_node the new head, but if the head // is no longer what's stored in new_node->next // (some other thread must have inserted a node just now) // then put that new head into new_node->next and try again while(!std::atomic_compare_exchange_weak_explicit( &head, &new_node->next, new_node, std::memory_order_release, std::memory_order_relaxed)) ; // the body of the loop is empty } }; int main() { stack<int> s; s.push(1); s.push(2); s.push(3); }
See also
atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not (public member function of std::atomic<T> )
| |
(C++11)(C++11) |
atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic (function template) |
specializes atomic operations for std::shared_ptr (function template) | |
C documentation for atomic_compare_exchange, atomic_compare_exchange_explicit
|