std::atomic_compare_exchange_strong
STD::原子[医]比较[医]交换[医]弱,STD::原子[医]比较[医]交换[医]强,STD::原子[医]比较[医]交换[医]弱[医]显式,STD::原子[医]比较[医]交换[医]强[医]显式
Defined in header | | |
---|---|---|
| (1) | (since C++11) |
template< class T > bool atomic_compare_exchange_weak( std::atomic<T>* obj, T* expected, T desired | | |
template< class T > bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj, T* expected, T desired | | |
| (2) | (since C++11) |
template< class T > bool atomic_compare_exchange_strong( std::atomic<T>* obj, T* expected, T desired | | |
template< class T > bool atomic_compare_exchange_strong( volatile std::atomic<T>* obj, T* expected, T desired | | |
| (3) | (since C++11) |
template< class T > bool atomic_compare_exchange_weak_explicit( std::atomic<T>* obj, T* expected, T desired, std::memory_order succ, std::memory_order fail | | |
template< class T > bool atomic_compare_exchange_weak_explicit( volatile std::atomic<T>* obj, T* expected, T desired, std::memory_order succ, std::memory_order fail | | |
| (4) | (since C++11) |
template< class T > bool atomic_compare_exchange_strong_explicit( std::atomic<T>* obj, T* expected, T desired, std::memory_order succ, std::memory_order fail | | |
template< class T > bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj, T* expected, T desired, std::memory_order succ, std::memory_order fail | |
原子比较对象表示所指向的对象的obj
所指向的对象的对象表示形式。expected
,好像std::memcmp
,如果这些是按位相等的,则将前者替换为desired
%28执行读-修改-写操作%29。否则,加载obj
进*expected
%28执行加载操作%29。复制就像std::memcpy
...
读-修改-写入和加载操作的内存模型包括succ
和fail
分别。%281-2%29版本使用std::memory_order_seq_cst
默认情况下。
的成员函数定义为std::atomic
*
1%29obj->compare_exchange_weak(*expected, desired)
2%29obj->compare_exchange_strong(*expected, desired)
3%29obj->compare_exchange_weak(*expected, desired, succ, fail)
4%29obj->compare_exchange_strong(*expected, desired, succ, fail)
参数
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 synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted. |
fail | - | the memory synchronization 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 (until C++17) |
返回值
比较结果:true
如果*obj
等于*expected
,,,false
否则。
例外
noexcept
规格:
noexcept
注记
弱窗体%28%281%29和%283%29%29允许伪造地失败,也就是说,就像*obj != *expected
即使他们是平等的。当比较和交换处于循环状态时,弱版本将在某些平台上获得更好的性能。
当弱比较和交换需要一个循环,而强比较和交换不需要循环时,强比较和交换就更好了,除非T
可以包括填充位、陷阱位,或者为相同值%28例如提供多个对象表示。浮点南%29.。在这些情况下,弱比较和交换通常是有效的,因为它能快速地收敛于某些稳定的对象表示上。
例
比较和交换操作通常用作无锁数据结构的基本构建块。
二次
#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
// note: the above loop is not thread-safe in at least
// GCC prior to 4.8.3 (bug 60272), clang prior to 2014-05-05 (bug 18899)
// MSVC prior to 2014-03-17 (bug 819819). See member function version for workaround
}
};
int main()
{
stack<int> s;
s.push(1
s.push(2
s.push(3
}
二次
另见
compare_exchange_weakcompare_exchange_strong | 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) |
---|---|
atomic_exchangeatomic_exchange_explicit (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) |
std::atomic_compare_exchange_weak(std::shared_ptr) std::atomic_compare_exchange_strong(std::shared_ptr) | specializes atomic operations for std::shared_ptr (function template) |
C原子文档[医]比较[医]交换,原子的[医]比较[医]交换[医]显式
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。