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 起) 和指针类型的特化 (类模板) |