首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::atomic_compare_exchange_weak_explicit

Defined in header <atomic>

?

?

?

(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...

读-修改-写入和加载操作的内存模型包括succfail分别。%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.。在这些情况下,弱比较和交换通常是有效的,因为它能快速地收敛于某些稳定的对象表示上。

比较和交换操作通常用作无锁数据结构的基本构建块。

二次

代码语言:javascript
复制
#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原子文档[医]比较[医]交换,原子的[医]比较[医]交换[医]显式

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com