简体   繁体   English

解锁另一个线程锁定的互斥锁

[英]Unlocking a mutex locked by another thread

Why does unlocking a mutex which is already kept locked by another thread cause undefined behavior? 为什么解锁已经被另一个线程锁定的互斥锁会导致未定义的行为?

According to http://www.cplusplus.com/reference/mutex/mutex/unlock/ , if, say, thread 1 locks a mutex, and thread 2 then tries to unlock the same mutex before it is unlocked, we incur in undefined behavior. 根据http://www.cplusplus.com/reference/mutex/mutex/unlock/的说明 ,如果说线程1锁定了一个互斥锁,然后线程2然后尝试在未锁定该互斥锁之前对其进行了解锁,则会导致未定义行为。

Looking at this documentation on the member function unlock() , which is the member function whose documentation you are linking: https://en.cppreference.com/w/cpp/thread/mutex/unlock 在成员函数unlock()上查看此文档,这是您要链接其文档的成员函数: https : //en.cppreference.com/w/cpp/thread/mutex/unlock

It states: 它指出:

std::mutex::unlock() std :: mutex :: unlock()

Unlocks the mutex. 解锁互斥锁。

The mutex must be locked by the current thread of execution, otherwise, the behavior is undefined . 互斥锁必须由当前执行线程锁定,否则, 该行为是undefined

So I think the documentation simply states that if a thread tries to unlock a mutex it does not own, we incur in undefined behavior. 因此,我认为文档只是指出,如果线程尝试解锁它不拥有的互斥锁,则会导致未定义的行为。

For what concerns the title of the question: locking a lock owned by another thread is a well defined behavior (it is why we have std::mutex ) https://en.cppreference.com/w/cpp/thread/mutex/lock : 对于与问题标题有关的问题:锁定另一个线程拥有的锁是一个明确定义的行为(这就是为什么我们拥有std::mutexhttps://en.cppreference.com/w/cpp/thread/mutex/锁

std::mutex::lock() std :: mutex :: lock()

Locks the mutex. 锁定互斥锁。 If another thread has already locked the mutex, a call to lock will block execution until the lock is acquired. 如果另一个线程已经锁定了互斥锁,则对锁的调用将阻止执行,直到获得该锁为止。

As a side note, directly unlocking a mutex is usually a bad idea, you should use RAII wrappers like std::lock_guard 附带说明,直接解锁互斥锁通常不是一个好主意,您应该使用RAII包装器,如std::lock_guard

The link you provided actually talks about double unlocking, not double locking. 您提供的链接实际上是关于双重解锁,而不是双重锁定。 However, the operation (ie unlocking the mutex from other thread, while owner thread is not locking it) is UB because the language does not define how a mutex lock should be implemented, thus (and this is pure conjecture) enabling a binary lock, which double unlock might actually lock... 但是,该操作(即从其他线程解锁互斥锁,而所有者线程未锁定互斥锁)是UB,因为该语言未定义应如何实现互斥锁,因此(这纯粹是推测)启用了二进制锁,哪个双重解锁实际上可能会锁定...

According to http://www.cplusplus.com/reference/mutex/mutex/unlock/ : 根据http://www.cplusplus.com/reference/mutex/mutex/unlock/

All lock and unlock operations on the mutex follow a single total order, with all visible effects synchronized between the lock operations and previous unlock operations on the same object. 互斥锁上的所有锁定和解锁操作均遵循单个总顺序,并且所有可见效果在锁定操作和同一对象的先前解锁操作之间同步。 If the mutex is not currently locked by the calling thread, it causes undefined behavior. 如果互斥锁当前未被调用线程锁定,则它将导致未定义的行为。

This simply means you should always (in the same thread) call lock before unlock . 这只是意味着您应始终(在同一线程中)在解锁之前调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM