【问题标题】:C++11: what's the difference between atomic<T>::store and atomic_store<T>C++11:atomic<T>::store 和 atomic_store<T> 有什么区别
【发布时间】:2018-01-02 05:08:56
【问题描述】:

一个是模板类std::atomic的成员函数,一个是模板函数,看起来他们做同样的事情。由于std是一个类库,为什么它同时提供了class和none-class两种版本,我觉得操作一样呢?

它们之间有什么真正的区别吗?

【问题讨论】:

    标签: c++ function class c++11 atomic


    【解决方案1】:

    语义上没有区别。免费函数是为了实现与 C11 的源代码兼容性:

    #ifdef __cplusplus
    #include <atomic>
    #define _Atomic(X) std::atomic<X>
    #else
    #include <stdatomic.h>
    #endif
    
    _Atomic(int) c;
    
    int get_c(void) { 
        return atomic_load(&c); 
    }
    

    【讨论】:

      【解决方案2】:

      就像你说的 - 一个是类,另一个是函数。类具有接口 - atomic&lt;T&gt; 将提供存储、加载、适当的构造函数等。

      另一方面,atomic_store 可以专门用于您的类型。

      【讨论】:

        猜你喜欢
        • 2021-06-21
        • 2012-03-10
        • 1970-01-01
        • 2021-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        相关资源
        最近更新 更多