• C++11 并发指南六(atomic 类型详解三 std::atomic (续))


    C++11 并发指南六( <atomic> 类型详解二 std::atomic ) 介绍了基本的原子类型 std::atomic 的用法,本节我会给大家介绍C++11 标准库中的 std::atomic 针对整形(integral)和指针类型的特化版本做了哪些改进。

    总地来说,C++11 标准库中的 std::atomic 针对整形(integral)和指针类型的特化版本新增了一些算术运算和逻辑运算操作。具体如下:

    integral fetch_add(integral, memory_order = memory_order_seq_cst) volatile;
    integral fetch_add(integral, memory_order = memory_order_seq_cst);
    integral fetch_sub(integral, memory_order = memory_order_seq_cst) volatile;
    integral fetch_sub(integral, memory_order = memory_order_seq_cst);
    integral fetch_and(integral, memory_order = memory_order_seq_cst) volatile;
    integral fetch_and(integral, memory_order = memory_order_seq_cst);
    integral fetch_or(integral, memory_order = memory_order_seq_cst) volatile;
    integral fetch_or(integral, memory_order = memory_order_seq_cst);
    integral fetch_xor(integral, memory_order = memory_order_seq_cst) volatile;
    integral fetch_xor(integral, memory_order = memory_order_seq_cst);
    
    integral operator++(int) volatile;
    integral operator++(int);
    integral operator--(int) volatile;
    integral operator--(int);
    integral operator++() volatile;
    integral operator++();
    integral operator--() volatile;
    integral operator--();
    integral operator+=(integral) volatile;
    integral operator+=(integral);
    integral operator-=(integral) volatile;
    integral operator-=(integral);
    integral operator&=(integral) volatile;
    integral operator&=(integral);
    integral operator|=(integral) volatile;
    integral operator|=(integral);
    integral operator^=(integral) volatile;
    integral operator^=(integral);
    

     下面我们来简单介绍以上的 std::atomic 特化版本的成员函数。

    if T is integral (1)
    T fetch_add (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_add (T val, memory_order sync = memory_order_seq_cst) noexcept;
    
    if T is pointer (2)
    T fetch_add (ptrdiff_t val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_add (ptrdiff_t val, memory_order sync = memory_order_seq_cst) noexcept;
    
    Memory Order 值Memory Order 类型
    memory_order_relaxed Relaxed
    memory_order_consume Consume
    memory_order_acquire Acquire
    memory_order_release Release
    memory_order_acq_rel Acquire/Release
    memory_order_seq_cst Sequentially consistent
    if T is integral (1)
    T fetch_sub (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_sub (T val, memory_order sync = memory_order_seq_cst) noexcept;
    
    if T is pointer (2)
    T fetch_sub (ptrdiff_t val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_sub (ptrdiff_t val, memory_order sync = memory_order_seq_cst) noexcept;
    
    Memory Order 值Memory Order 类型
    memory_order_relaxed Relaxed
    memory_order_consume Consume
    memory_order_acquire Acquire
    memory_order_release Release
    memory_order_acq_rel Acquire/Release
    memory_order_seq_cst Sequentially consistent
    T fetch_and (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_and (T val, memory_order sync = memory_order_seq_cst) noexcept;
    
    Memory Order 值Memory Order 类型
    memory_order_relaxed Relaxed
    memory_order_consume Consume
    memory_order_acquire Acquire
    memory_order_release Release
    memory_order_acq_rel Acquire/Release
    memory_order_seq_cst Sequentially consistent
    T fetch_or (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_or (T val, memory_order sync = memory_order_seq_cst) noexcept;
    
    Memory Order 值Memory Order 类型
    memory_order_relaxed Relaxed
    memory_order_consume Consume
    memory_order_acquire Acquire
    memory_order_release Release
    memory_order_acq_rel Acquire/Release
    memory_order_seq_cst Sequentially consistent
    T fetch_xor (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
    T fetch_xor (T val, memory_order sync = memory_order_seq_cst) noexcept;
    
    Memory Order 值Memory Order 类型
    memory_order_relaxed Relaxed
    memory_order_consume Consume
    memory_order_acquire Acquire
    memory_order_release Release
    memory_order_acq_rel Acquire/Release
    memory_order_seq_cst Sequentially consistent
    pre-increment (1)
    T operator++() volatile noexcept;
    T operator++() noexcept;
    
    post-increment (2)
    T operator++ (int) volatile noexcept;
    T operator++ (int) noexcept;
    
    if T is integral (1)
    T operator+= (T val) volatile noexcept;
    T operator+= (T val) noexcept;
    T operator-= (T val) volatile noexcept;
    T operator-= (T val) noexcept;
    T operator&= (T val) volatile noexcept;
    T operator&= (T val) noexcept;
    T operator|= (T val) volatile noexcept;
    T operator|= (T val) noexcept;
    T operator^= (T val) volatile noexcept;
    T operator^= (T val) noexcept;
    
    if T is pointer (2)
    T operator+= (ptrdiff_t val) volatile noexcept;
    T operator+= (ptrdiff_t val) noexcept;
    T operator-= (ptrdiff_t val) volatile noexcept;
    T operator-= (ptrdiff_t val) noexcept;
    

    以上各个 operator 都会有对应的 fetch_* 操作,详细见下表:

    操作符成员函数支持类型
    复合赋值等价于整型指针类型其他类型
    + atomic::operator+= atomic::fetch_add
    - atomic::operator-= atomic::fetch_sub
    & atomic::operator&= atomic::fetch_and
    | atomic::operator|= atomic::fetch_or
    ^ atomic::operator^= atomic::fetch_xor

    好了,本节先介绍这里,下一节我会介绍 C++11 中 C 风格的原子操作 API。

  • 相关阅读:
    Linux 相关scsi命令
    存储
    Multipath多路径冗余全解析
    Oracle中alter system命令参数之scope
    scipy安装失败
    font
    查看端口占用
    oracle参数优化
    组播
    sql给整数补零
  • 原文地址:https://www.cnblogs.com/haippy/p/3304556.html
Copyright © 2020-2023  润新知