如果我们已经手动解锁,unique_lock 是否会在销毁时解锁?
Is unique_lock unlocked on destruction if we already unlocked it by hand?
即使我手动调用 unlock
,unique_lock::unlock()
函数是否会在销毁时调用,以便在给定的互斥体上收到两次解锁信号?
没有。
见 documentation:
If *this
has an associated mutex and has acquired ownership of it, the
mutex is unlocked.
不,它不会尝试解锁。如果您查看 unlock
,它
Ensures: owns == false
.
Effects: If owns
calls pm->unlock()
.
因此,由于 unlock
释放了所有权,析构函数将不会尝试解锁它。
不是这样。
考虑mutex
的观点,其中std::mutex::unlock
(来自cppreference):
Unlocks the mutex.
The mutex must be locked by the current thread of execution,
otherwise, the behavior is undefined.
因此,如果锁在不再锁定的 mutex
上调用 unlock
,那将是致命的。
即使我手动调用 unlock
,unique_lock::unlock()
函数是否会在销毁时调用,以便在给定的互斥体上收到两次解锁信号?
没有。 见 documentation:
If
*this
has an associated mutex and has acquired ownership of it, the mutex is unlocked.
不,它不会尝试解锁。如果您查看 unlock
,它
Ensures:
owns == false
.
Effects: If
owns
callspm->unlock()
.
因此,由于 unlock
释放了所有权,析构函数将不会尝试解锁它。
不是这样。
考虑mutex
的观点,其中std::mutex::unlock
(来自cppreference):
Unlocks the mutex.
The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined.
因此,如果锁在不再锁定的 mutex
上调用 unlock
,那将是致命的。