std::atomic<std::weak_ptr>
| 定義於標頭檔案 <memory> |
||
| template< class T > struct std::atomic<std::weak_ptr<T>>; |
(C++20 起) | |
std::atomic 的 std::weak_ptr<T> 偏特化允許使用者原子地操作 weak_ptr 物件。
如果多個執行執行緒在不同步的情況下訪問同一個 std::weak_ptr 物件,並且其中任何一個訪問使用了 weak_ptr 的非 const 成員函式,那麼將會發生資料競爭,除非所有此類訪問都透過 std::atomic<std::weak_ptr> 的例項執行。
相關的 use_count 增量保證是原子操作的一部分。相關的 use_count 減量在原子操作之後進行排序,但不需要是原子操作的一部分,除了在失敗的 CAS 中覆蓋 expected 時 use_count 的變化。任何相關的刪除和釋放都在原子更新步驟之後進行排序,並且不屬於原子操作的一部分。
請注意,std::weak_ptr 和 std::shared_ptr 使用的控制塊是執行緒安全的:不同的非原子 std::weak_ptr 物件可以透過可變操作(例如 operator= 或 reset)同時被多個執行緒訪問,即使這些例項是副本或在內部共享相同的控制塊。
型別 T 可以是不完整型別。
[編輯] 成員型別
| 成員型別 | 定義 |
value_type
|
std::weak_ptr<T> |
[編輯] 成員函式
此特化還提供了所有非特化 std::atomic 函式,並且沒有額外的成員函式。
atomic<weak_ptr<T>>::atomic
| constexpr atomic() noexcept = default; |
(1) | |
| atomic(std::weak_ptr<T> desired) noexcept; |
(2) | |
| atomic(const atomic&) = delete; |
(3) | |
weak_ptr<T> 初始化為預設構造的值。atomic<weak_ptr<T>>::operator=
| void operator=(const atomic&) = delete; |
(1) | |
| void operator=(std::weak_ptr<T> desired) noexcept; |
(2) | |
atomic<weak_ptr<T>>::is_lock_free
| bool is_lock_free() const noexcept; |
||
如果此型別所有物件的原子操作都是無鎖的,則返回 true,否則返回 false。
atomic<weak_ptr<T>>::store
| void store(std::weak_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst) noexcept; |
||
原子地將 *this 的值替換為 desired 的值,如同透過 p.swap(desired),其中 p 是底層 std::weak_ptr<T>。記憶體按 order 排序。如果 order 是 std::memory_order_consume、std::memory_order_acquire 或 std::memory_order_acq_rel,則行為未定義。
atomic<weak_ptr<T>>::load
| std::weak_ptr<T> load(std::memory_order order = std::memory_order_seq_cst) const noexcept; |
||
原子地返回底層 std::weak_ptr<T> 的副本。記憶體按 order 排序。如果 order 是 std::memory_order_release 或 std::memory_order_acq_rel,則行為未定義。
atomic<weak_ptr<T>>::operator std::weak_ptr<T>
| operator std::weak_ptr<T>() const noexcept; |
||
等同於 return load();。
atomic<weak_ptr<T>>::exchange
| std::weak_ptr<T> exchange(std::weak_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst) noexcept; |
||
原子地將底層 std::weak_ptr<T> 替換為 desired,如同透過 p.swap(desired),其中 p 是底層 std::weak_ptr<T>,並返回 p 在交換之前立即持有的值的副本。記憶體按 order 排序。這是一個原子讀-改-寫操作。
atomic<weak_ptr<T>>::compare_exchange_weak, compare_exchange_strong
| bool compare_exchange_strong(std::weak_ptr<T>& expected, std::weak_ptr<T> desired, std::memory_order success, std::memory_order failure) noexcept; |
(1) | |
| bool compare_exchange_weak(std::weak_ptr<T>& expected, std::weak_ptr<T> desired, std::memory_order success, std::memory_order failure) noexcept; |
(2) | |
| bool compare_exchange_strong(std::weak_ptr<T>& expected, std::weak_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst) noexcept; |
(3) | |
| bool compare_exchange_weak(std::weak_ptr<T>& expected, std::weak_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst) noexcept; |
(4) | |
expected 相同的指標值並與其共享所有權,或者底層和 expected 都為空,則將 desired 賦值給底層 std::weak_ptr<T>,返回 true,並根據 success 排序記憶體,否則將底層 std::weak_ptr<T> 賦值給 expected,返回 false,並根據 failure 排序記憶體。如果 failure 是 std::memory_order_release 或 std::memory_order_acq_rel,則行為未定義。成功時,該操作是 *this 上的原子讀-改-寫操作,並且 expected 在原子更新後不會被訪問。失敗時,該操作是 *this 上的原子載入操作,並且 expected 會用從原子物件讀取的現有值進行更新。對 expected 的 use_count 的此更新是此原子操作的一部分,儘管寫入本身(以及任何後續的釋放/銷燬)不要求是。fail_order 與 order 相同,但 std::memory_order_acq_rel 被 std::memory_order_acquire 替換,std::memory_order_release 被 std::memory_order_relaxed 替換。fail_order 與 order 相同,但 std::memory_order_acq_rel 被 std::memory_order_acquire 替換,std::memory_order_release 被 std::memory_order_relaxed 替換。atomic<weak_ptr<T>>::wait
| void wait(std::weak_ptr<T> old std::memory_order order = std::memory_order_seq_cst) const noexcept; |
||
執行原子等待操作。
將 load(order) 與 old 進行比較,如果它們等效,則阻塞直到 *this 被 notify_one() 或 notify_all() 通知。此過程重複進行,直到 load(order) 發生變化。即使底層實現虛假解除阻塞,此函式也保證僅在值發生變化時返回。
記憶體按 order 排序。如果 order 是 std::memory_order_release 或 std::memory_order_acq_rel,則行為未定義。
注意:兩個 std::weak_ptrs 如果它們儲存相同的指標並共享所有權或兩者都為空,則它們是等效的。
atomic<weak_ptr<T>>::notify_one
| void notify_one() noexcept; |
||
執行原子通知操作。
如果存在一個執行緒被阻塞在 *this 的原子等待操作(即 wait())中,則至少解除阻塞一個這樣的執行緒;否則不執行任何操作。
atomic<weak_ptr<T>>::notify_all
| void notify_all() noexcept; |
||
執行原子通知操作。
解除阻塞所有被阻塞在 *this 的原子等待操作(即 wait())中的執行緒(如果存在);否則不執行任何操作。
[編輯] 成員常量
此特化也提供了唯一的標準 std::atomic 成員常量 is_always_lock_free。
atomic<weak_ptr<T>>::is_always_lock_free
| static constexpr bool is_always_lock_free = /*實現定義*/; |
||
[編輯] 示例
| 本節不完整 原因:無示例 |
[編輯] 另請參閱
| (C++11) |
atomic 類模板和針對 bool、整型、浮點型(C++20 起) 和指標型別的特化 (類模板) |