C++11 併發指南六( <atomic> 類型詳解二 std::atomic ) 介紹了基本的原子類型 std::atomic 的用法,本節我會給你們介紹C++11 標準庫中的 std::atomic 針對整形(integral)和指針類型的特化版本作了哪些改進。html
總地來講,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_* 操做,詳細見下表:post
操做符 | 成員函數 | 支持類型 | |||
---|---|---|---|---|---|
複合賦值 | 等價於 | 整型 | 指針類型 | 其餘類型 | |
+ |
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。fetch