Namespaces
Variants

std::unique_lock<Mutex>:: operator=

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
unique_lock & operator = ( unique_lock && other ) noexcept ;
(C++11 起)

移动赋值运算符。等价于 unique_lock { std :: move ( other ) } . swap ( * this ) ; return * this ;

如果 other * this 是同一对象,则无任何效果。否则,若在调用前 * this 已关联互斥锁并获取其所有权,则该互斥锁将被解锁。

目录

参数

其他 - 用于替换状态的另一个 unique_lock

返回值

* this

注释

对于递归互斥锁,在赋值操作前 * this other 可能同时拥有同一个互斥锁。在这种情况下,赋值操作后 * this 将持有该互斥锁,而 other 将不再持有。

移动赋值操作可能引发未定义行为。例如,当 * this 通过 std::adopt_lock 构造时,若调用线程不持有相关互斥锁的所有权,则无法正确释放该互斥锁的所有权。

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 2104 C++11 移动赋值运算符被声明为 noexcept 但可能导致未定义行为 移除 noexcept 声明
LWG 4172 C++11 LWG2104 移除了 noexcept
unique_lock 的自移动赋值规范存在错误
恢复 noexcept 声明
重新规范为无操作