前面三篇文章《C++11 併發指南六(atomic 類型詳解一 atomic_flag 介紹)》、《C++11 併發指南六( <atomic> 類型詳解二 std::atomic )》、《C++11 併發指南六(atomic 類型詳解三 std::atomic (續))》都是採用 C++ 的方式介紹原子對象,本節我會給你們介紹 C++11 原子操做中 C 風格的 API。html
總地來講,C++11 標準中規定了兩大類原子對象,std::atomic_flag 和 std::atomic,前者 std::atomic_flag 一種最簡單的原子布爾類型,只支持兩種操做,test-and-set 和 clear。而 std::atomic 是模板類,一個模板類型爲 T 的原子對象中封裝了一個類型爲 T 的值,而且C++11 標準中除了定義基本 std::atomic 模板類型外,還提供了針對整形(integral)和指針類型的特化實現,提供了大量的 API,極大地方便了開發者使用。下面我分別介紹基於 std::atomic_flag 和 std::atomic 的 C 風格 API。算法
bool atomic_flag_test_and_set (volatile atomic_flag* obj) noexcept; bool atomic_flag_test_and_set (atomic_flag* obj) noexcept;
bool atomic_flag_test_and_set (volatile atomic_flag* obj, memory_order sync) noexcept; bool atomic_flag_test_and_set (atomic_flag* obj, memory_order sync) 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 |
void atomic_flag_clear (volatile atomic_flag* obj) noexcept; void atomic_flag_clear (atomic_flag* obj) noexcept;
void atomic_flag_clear (volatile atomic_flag* obj, memory_order sync) noexcept; void atomic_flag_clear (atomic_flag* obj, memory_order sync) 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 |
template (1) | template <class T> bool atomic_is_lock_free (const volatile atomic<T>* obj) noexcept; template <class T> bool atomic_is_lock_free (const atomic<T>* obj) noexcept; |
---|---|
overloads (2) | bool atomic_is_lock_free (const volatile A* obj) noexcept; bool atomic_is_lock_free (const A* obj) noexcept; |
template (1) | template <class T> void atomic_init (volatile atomic<T>* obj, T val) noexcept; template <class T> void atomic_init (atomic<T>* obj, T val) noexcept; |
---|---|
overloads (2) | void atomic_init (volatile A* obj, T val) noexcept; void atomic_init (A* obj, T val) noexcept; |
template (1) | template <class T> void atomic_store (volatile atomic<T>* obj, T val) noexcept; template <class T> void atomic_store (atomic<T>* obj, T val) noexcept; |
---|---|
overloads (2) | void atomic_store (volatile A* obj, T val) noexcept; void atomic_store (A* obj, T val) noexcept; |
Memory Order 值 | Memory Order 類型 |
---|---|
memory_order_relaxed | Relaxed |
memory_order_release | Release |
memory_order_seq_cst | Sequentially consistent |
template (1) | template <class T> T atomic_load (const volatile atomic<T>* obj) noexcept; template <class T> T atomic_load (const atomic<T>* obj) noexcept; |
---|---|
overloads (2) | T atomic_load (const volatile A* obj) noexcept; T atomic_load (const A* obj) noexcept; |
template (1) | template <class T> T atomic_load_explicit (const volatile atomic<T>* obj, memory_order sync) noexcept; template <class T> T atomic_load_explicit (const atomic<T>* obj, memory_order sync) noexcept; |
---|---|
overloads (2) | T atomic_load_explicit (const volatile A* obj, memory_order sync) noexcept; T atomic_load_explicit (const A* obj, memory_order sync) noexcept; |
Memory Order 值 | Memory Order 類型 |
---|---|
memory_order_relaxed | Relaxed |
memory_order_consume | Consume |
memory_order_acquire | Acquire |
memory_order_seq_cst | Sequentially consistent |
template (1) | template <class T> T atomic_exchange (volatile atomic<T>* obj, T val) noexcept; template <class T> T atomic_exchange (atomic<T>* obj, T val) noexcept; |
---|---|
overloads (2) | T atomic_exchange (volatile A* obj, T val) noexcept; T atomic_exchange (A* obj, T val) noexcept; |
template (1) | template <class T> T atomic_store_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept; template <class T> T atomic_store_explicit (atomic<T>* obj, T val, memory_order sync) noexcept; |
---|---|
overloads (2) | T atomic_store_explicit (volatile A* obj, T val, memory_order sync) noexcept; T atomic_store_explicit (A* obj, T val, memory_order sync) 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 |
template (1) | template <class T> bool atomic_compare_exchange_weak (volatile atomic<T>* obj, T* expected, T val) noexcept; template <class T> bool atomic_compare_exchange_weak (atomic<T>* obj, T* expected, T val) noexcept; |
---|---|
overloads (2) | bool atomic_compare_exchange_weak (volatile A* obj, T* expected, T val) noexcept; bool atomic_compare_exchange_weak (A* obj, T* expected, T val) noexcept; |
expected 的值不會改變。
template (1) | template <class T> bool atomic_compare_exchange_weak_explicit (volatile atomic<T>* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; template <class T> bool atomic_compare_exchange_weak_explicit (atomic<T>* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; |
---|---|
overloads (2) | bool atomic_compare_exchange_weak_explicit (volatile A* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; bool atomic_compare_exchange_weak_explicit (A* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; |
expected 的值不會改變。
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 |
template (1) | template <class T> bool atomic_compare_exchange_strong (volatile atomic<T>* obj, T* expected, T val) noexcept; template <class T> bool atomic_compare_exchange_strong (atomic<T>* obj, T* expected, T val) noexcept; |
---|---|
overloads (2) | bool atomic_compare_exchange_strong (volatile A* obj, T* expected, T val) noexcept; bool atomic_compare_exchange_strong (A* obj, T* expected, T val) noexcept; |
template (1) | template <class T> bool atomic_compare_exchange_strong_explicit (volatile atomic<T>* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; template <class T> bool atomic_compare_exchange_strong_explicit (atomic<T>* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; |
---|---|
overloads (2) | bool atomic_compare_exchange_strong_explicit (volatile A* obj, T* expected, T val, memory_order success, memory_order failure) noexcept; bool atomic_compare_exchange_strong_explicit (A* obj, T* expected, T val, memory_order success, memory_order failure) 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 |
template (integral) (1) | template <class T> T atomic_fetch_add (volatile atomic<T>* obj, T val) noexcept; template <class T> T atomic_fetch_add (atomic<T>* obj, T val) noexcept; |
---|---|
template (pointer) (2) | template <class U> U* atomic_fetch_add (volatile atomic<U*>* obj, ptrdiff_t val) noexcept; template <class U> U* atomic_fetch_add (atomic<U*>* obj, ptrdiff_t val) noexcept; |
overloads (3) | T atomic_fetch_add (volatile A* obj, M val) noexcept; T atomic_fetch_add (A* obj, M val) noexcept; |
template (integral) (1) | template <class T> T atomic_fetch_add_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept; template <class T> T atomic_fetch_add_explicit (atomic<T>* obj, T val, memory_order sync) noexcept; |
---|---|
template (pointer) (2) | template <class U> U* atomic_fetch_add_explicit (volatile atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept; template <class U> U* atomic_fetch_add_explicit (atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept; |
overloads (3) | T atomic_fetch_add_explicit (volatile A* obj, M val, memory_order sync) noexcept; T atomic_fetch_add_explicit (A* obj, M val, memory_order sync) 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 |
template (integral) (1) | template <class T> T atomic_fetch_sub (volatile atomic<T>* obj, T val) noexcept; template <class T> T atomic_fetch_sub (atomic<T>* obj, T val) noexcept; |
---|---|
template (pointer) (2) | template <class U> U* atomic_fetch_sub (volatile atomic<U*>* obj, ptrdiff_t val) noexcept; template <class U> U* atomic_fetch_sub (atomic<U*>* obj, ptrdiff_t val) noexcept; |
overloads (3) | T atomic_fetch_sub (volatile A* obj, M val) noexcept; T atomic_fetch_sub (A* obj, M val) noexcept; |
template (integral) (1) | template <class T> T atomic_fetch_sub_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept; template <class T> T atomic_fetch_sub_explicit (atomic<T>* obj, T val, memory_order sync) noexcept; |
---|---|
template (pointer) (2) | template <class U> U* atomic_fetch_sub_explicit (volatile atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept; template <class U> U* atomic_fetch_sub_explicit (atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept; |
overloads (3) | T atomic_fetch_sub_explicit (volatile A* obj, M val, memory_order sync) noexcept; T atomic_fetch_sub_explicit (A* obj, M val, memory_order sync) 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 |
emplate (integral) (1) | template <class T> T atomic_fetch_and (volatile atomic<T>* obj, T val) noexcept; template <class T> T atomic_fetch_and (atomic<T>* obj, T val) noexcept; |
---|---|
overloads (2) | T atomic_fetch_and (volatile A* obj, T val) noexcept; T atomic_fetch_and (A* obj, T val) noexcept; |
template (integral) (1) | template <class T> T atomic_fetch_and_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept; template <class T> T atomic_fetch_and_explicit (atomic<T>* obj, T val, memory_order sync) noexcept; |
---|---|
overloads (2) | T atomic_fetch_and_explicit (volatile A* obj, T val, memory_order sync) noexcept; T atomic_fetch_and_explicit (A* obj, T val, memory_order sync) 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 |
template (integral) (1) | template <class T> T atomic_fetch_or (volatile atomic<T>* obj, T val) noexcept; template <class T> T atomic_fetch_or (atomic<T>* obj, T val) noexcept; |
---|---|
overloads (2) | T atomic_fetch_or (volatile A* obj, T val) noexcept; T atomic_fetch_or (A* obj, T val) noexcept; |
template (integral) (1) | template <class T> T atomic_fetch_or_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept; template <class T> T atomic_fetch_or_explicit (atomic<T>* obj, T val, memory_order sync) noexcept; |
---|---|
overloads (2) | T atomic_fetch_or_explicit (volatile A* obj, T val, memory_order sync) noexcept; T atomic_fetch_or_explicit (A* obj, T val, memory_order sync) 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 |
template (integral) (1) | template <class T> T atomic_fetch_xor (volatile atomic<T>* obj, T val) noexcept; template <class T> T atomic_fetch_xor (atomic<T>* obj, T val) noexcept; |
---|---|
overloads (2) | T atomic_fetch_xor (volatile A* obj, T val) noexcept; T atomic_fetch_xor (A* obj, T val) noexcept; |
template (integral) (1) | template <class T> T atomic_fetch_xor_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept; template <class T> T atomic_fetch_xor_explicit (atomic<T>* obj, T val, memory_order sync) noexcept; |
---|---|
overloads (2) | T atomic_fetch_xor_explicit (volatile A* obj, T val, memory_order sync) noexcept; T atomic_fetch_xor_explicit (A* obj, T val, memory_order sync) 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 |