std::atomic(std::weak_ptr)
template <class T> struct std::atomic<std::weak_ptr<T>>;
std::atomic 对 std::weak_ptr<T> 的偏特化允许用户原子地操作weak_ptr 对象。
如果多个执行线程在没有同步的情况下访问同一个 std::weak_ptr 对象,并且其中任何一个访问都使用了 weak_ptr 的非 const 成员函数,则会发生数据竞争,除非所有此类访问都通过 std::atomic<std::weak_ptr> 实例执行。
相关的 use_count 递增操作保证是原子操作的一部分。相关的 use_count 递减操作在原子操作之后排序,但不要求是原子操作的一部分,除了在 CAS 失败时覆盖预期值的 use_count 更改。任何相关的删除和解分配操作在原子更新步骤之后排序,并且不是原子操作的一部分。
请注意,std::weak_ptr 和 std::shared_ptr 使用的控制块是线程安全的:即使这些实例是副本或内部共享相同的控制块,多个线程也可以同时使用可变操作(如 operator= 或 reset)访问不同的非原子 std::weak_ptr 对象。
类型 T 可以是不完整类型。
成员类型
pub | 成员类型 | 定义 |
pub | 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> 初始化为默认构造的值。
- 将底层 weak_ptr<T> 初始化为 desired 的副本。与任何 std::atomic 类型一样,初始化不是原子操作。
- 原子类型不可复制/移动构造。
atomic<weak_ptr<T>>::operator=
void operator=(const atomic&) = delete; (1)
void operator=(std::weak_ptr<T> desired) noexcept; (2)
- 原子类型不可复制/移动赋值。
- 值赋值,等同于
store(desired)
。
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)
-
如果底层
std::weak_ptr<T>
存储与 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 的此更新是此原子操作的一部分,尽管写入本身(以及任何后续的解分配/销毁)不要求是。 -
与 (1) 相同,但可能也会意外失败。
-
等同于:
return compare_exchange_strong(expected, desired, order, fail_order);
,其中 fail_order 与 order 相同,但 std::memory_order_acq_rel 被替换为 std::memory_order_acquire,std::memory_order_release 被替换为 std::memory_order_relaxed。 -
等同于:
return compare_exchange_weak(expected, desired, order, fail_order);
,其中 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,如果它们相等,则阻塞直到 *thi
s 被 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 = /*implementation-defined*/;
执行原子通知操作。
解除阻塞所有在 *this
上被阻塞的线程(即 wait()
),如果存在的话;否则不执行任何操作。