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_ptr 存储相同的指针并且要么共享所有权,要么都为空,则它们是等效的。
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、integral、浮点,(C++20 起) 和指针类型的特化 (类模板) |