std::atomic<std::weak_ptr>

来自cppreference.com
 
 
内存管理库
分配器
内存资源
未初始化存储 (C++20 前*)
垃圾收集器支持 (C++23 前)
 
 
在标头 <memory> 定义
template< class T >
struct std::atomic<std::weak_ptr<T>>;
(C++20 起)

std::atomicstd::weak_ptr<T> 的部分模板特化允许用户原子地操纵 weak_ptr 对象。T 可以是不完整类型。

如果多个执行线程访问同一 std::weak_ptr 对象而不同步,而这些访问有任何一个使用的是 weak_ptr 的非 const 成员函数,除非所有这种访问都通过 std::atomic<std::weak_ptr> 的实例进行,否则将出现数据竞争。

保证关联 use_count 的自增是原子操作的一部分。要求关联 use_count 的自减在原子操作之后,但不要求是其一部分,除非是覆写失败的 CAS 中的 expected 时的 use_count 更改。任何关联的删除和解分配都后序于原子更新步骤,且不是原子操作的一部分。

注意 std::weak_ptrstd::shared_ptr 所用的控制块是线程安全的:多个线程能同时用可变操作,例如 operator=reset,访问不同的非原子 std::weak_ptr 对象,即使在这些实例互为副本,或因其他原因于内部共享同一控制块时也是如此。

嵌套类型

类型 定义
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) (C++26 起为 constexpr)
atomic( const atomic& ) = delete;
(3)
1) 初始化底层 weak_ptr<T> 为默认构造值。
2) 初始化底层 weak_ptr<T>desired 的副本。同任何 std::atomic 一样,初始化不是原子操作。
3) 原子类型不可复制/移动构造。

atomic<weak_ptr<T>>::operator=

void operator=( const atomic& ) = delete;
(1)
void operator=( std::weak_ptr<T> desired ) noexcept;
(2) (C++26 起为 constexpr)
1) 原子类型不可复制/移动赋值。
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;
(C++26 起为 constexpr)

如同用 p.swap(desired),原子地以 desired 的值替换 *this 的值,其中 p 是底层的 std::weak_ptr<T>。按照 order 排序内存。

如果 orderstd::memory_order_consumestd::memory_order_acquirestd::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;
(C++26 起为 constexpr)

原子地返回底层 std::weak_ptr<T> 的副本。按照 order 排序内存。

如果 orderstd::memory_order_releasestd::memory_order_acq_rel,那么行为未定义。

atomic<weak_ptr<T>>::operator std::weak_ptr<T>

operator std::weak_ptr<T>() const noexcept;
(C++26 起为 constexpr)

等价于 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;
(C++26 起为 constexpr)

如同用 p.swap(desired),原子地以 desired 替换底层 std::weak_ptr<T>,其中 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) (C++26 起为 constexpr)
bool compare_exchange_weak
    ( std::weak_ptr<T>& expected, std::weak_ptr<T> desired,
      std::memory_order success, std::memory_order failure ) noexcept;
(2) (C++26 起为 constexpr)
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) (C++26 起为 constexpr)
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) (C++26 起为 constexpr)
1) 如果底层 std::weak_ptr<T> 存储与 expected 相同的指针值并与之共享所有权,或底层指针和 expected 均为空,那么从 desired 赋值给底层 std::weak_ptr<T> 并返回 true,按照 success 排序内存;否则从底层 std::weak_ptr<T> 赋值给 expected 并返回 false,按照 failure 排序内存。
  • 成功时,操作是 *this 上的原子读修改写操作,且在原子更新后不会访问 expected
  • 失败时,操作是 *this 上的原子加载操作,并以读取自原子对象的既存值更新 expected。这个对 expecteduse_count 的更新是原子操作,尽管不要求写入自身(和任何后继的解分配/析构)是原子操作。
如果 failurestd::memory_order_releasestd::memory_order_acq_rel,那么行为未定义。
2)(1),但可能虚假地失败。
3) 等价于 return compare_exchange_strong(expected, desired, order, fail_order);,其中 fail_orderorder 相同,但 std:memory_order_acq_rel 被替换为 std::memory_order_acquire,而 std::memory_order_release 被替换为 std::memory_order_relaxed
4) 等价于 return compare_exchange_weak(expected, desired, order, fail_order);,其中 fail_orderorder 相同,但 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;
(C++26 起为 constexpr)

进行原子等待操作。

比较 load(order)old,且若它们等价则阻塞直至 *thisnotify_one()notify_all() 提醒。重复此操作直至 load(order) 更改。此函数保证只有在值更改的情况下才返回,即使底层实现虚假地除阻也是如此。按照 order 排序内存。

如果 orderstd::memory_order_releasestd::memory_order_acq_rel,那么行为未定义。

注解:如果两个 weak_ptr 存储相同指针,且要么共享所有权要么都为空,那么它们等价。

atomic<weak_ptr<T>>::notify_one

void notify_one() noexcept;
(C++26 起为 constexpr)

进行原子提醒操作。

如果有一个线程在 *this 上的原子等待操作(即 wait())中阻塞,那么除阻至少一个这种线程,否则不做任何事。

atomic<weak_ptr<T>>::notify_all

void notify_all() noexcept;
(C++26 起为 constexpr)

进行原子提醒操作。

除阻所有在 *this 上的原子等待操作(即 wait())中阻塞的线程(如果存在)。

成员常量

此特化也提供标准 std::atomic 仅有的成员常量 is_always_lock_free

atomic<weak_ptr<T>>::is_always_lock_free

static constexpr bool is_always_lock_free = /* 由实现定义 */;

注解

功能特性测试 标准 功能特性
__cpp_lib_constexpr_memory 202506L (C++26) constexpr 的 std::atomic<std::weak_ptr>

示例

参阅

(C++11)
atomic 类模板及其针对布尔、整数、浮点数(C++20 起)和指针类型的特化
(类模板) [编辑]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.