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

std::atomic::compare_exchange_strong

Defined in header <atomic>

?

?

?

(1)

(since C++11)

bool compare_exchange_weak( T& expected, T desired, std::memory_order success, std::memory_order failure );

?

bool compare_exchange_weak( T& expected, T desired, std::memory_order success, std::memory_order failure ) volatile;

?

?

(2)

(since C++11)

bool compare_exchange_weak( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst );

?

bool compare_exchange_weak( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst ) volatile;

?

?

(3)

(since C++11)

bool compare_exchange_strong( T& expected, T desired, std::memory_order success, std::memory_order failure );

?

bool compare_exchange_strong( T& expected, T desired, std::memory_order success, std::memory_order failure ) volatile;

?

?

(4)

(since C++11)

bool compare_exchange_strong( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst );

?

bool compare_exchange_strong( T& expected, T desired, std::memory_order order = std::memory_order_seq_cst ) volatile;

?

原子比较对象表示成*this的对象表示形式expected,好像std::memcmp,如果这些是按位相等的,则将前者替换为desired%28执行读-修改-写操作%29。否则,加载存储在*thisexpected%28执行加载操作%29。复制就像std::memcpy...

读-修改-写入和加载操作的内存模型包括successfailure分别。在%282%29和%284%29版本中order用于读-修改-写入和加载操作,但std::memory_order_acquirestd::memory_order_relaxed用于加载操作,如果order ==std::memory_order_acq_rel,或order ==std::memory_order_release分别。

参数

expected

-

reference 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

success

-

the memory synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted.

failure

-

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 success (until C++17)

order

-

the memory synchronization ordering for both operations

返回值

true如果成功地更改了基础原子值,false否则。

例外

noexcept规格:

noexcept

注记

弱窗体%281-2%29的函数被允许伪造地失败,也就是说,表现为*this != expected即使他们是平等的。当比较和交换处于循环状态时,弱版本将在某些平台上获得更好的性能。

当弱比较和交换需要一个循环,而强比较和交换不需要循环时,强比较和交换就更好了,除非T可以包括填充位、陷阱位,或者为相同值%28例如提供多个对象表示。浮点南%29.。在这些情况下,弱比较和交换通常是有效的,因为它能快速地收敛于某些稳定的对象表示上。

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

二次

代码语言:javascript
复制
#include <atomic>
template<typename T>
struct node
{
    T data;
    node* next;
    node(const T& data) : data(data), next(nullptr) {}
};
 
template<typename 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(!head.compare_exchange_weak(new_node->next, new_node,
                                        std::memory_order_release,
                                        std::memory_order_relaxed))
          ; // the body of the loop is empty
 
// Note: the above use 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). The following is a workaround:
//      node<T>* old_head = head.load(std::memory_order_relaxed);
//      do {
//          new_node->next = old_head;
//       } while(!head.compare_exchange_weak(old_head, new_node,
//                                           std::memory_order_release,
//                                           std::memory_order_relaxed));
    }
};
int main()
{
    stack<int> s;
    s.push(1);
    s.push(2);
    s.push(3);
}

二次

演示如何比较[医]交换[医]强可以更改原子变量或用于比较的变量的值。

二次

代码语言:javascript
复制
#include <atomic>
#include <iostream>
 
std::atomic<int>  ai;
 
int  tst_val= 4;
int  new_val= 5;
bool exchanged= false;
 
void valsout()
{
    std::cout << "ai= " << ai
              << "  tst_val= " << tst_val
              << "  new_val= " << new_val
              << "  exchanged= " << std::boolalpha << exchanged
              << "\n";
}
 
int main()
{
    ai= 3;
    valsout();
 
    // tst_val != ai   ==>  tst_val is modified
    exchanged= ai.compare_exchange_strong( tst_val, new_val );
    valsout();
 
    // tst_val == ai   ==>  ai is modified
    exchanged= ai.compare_exchange_strong( tst_val, new_val );
    valsout();
}

二次

产出:

二次

代码语言:javascript
复制
ai= 3  tst_val= 4  new_val= 5  exchanged= false
ai= 3  tst_val= 3  new_val= 5  exchanged= false
ai= 5  tst_val= 3  new_val= 5  exchanged= true

二次

另见

atomic_compare_exchange_weakatomic_compare_exchange_weak_explicitatomic_compare_exchange_strongatomic_compare_exchange_strong_explicit (C++11)(C++11)(C++11)(C++11)

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)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com