简体   繁体   English

当获取它的线程退出时,Mutex会发生什么?

[英]What happens to Mutex when the thread which acquired it exits?

Suppose there are two threads, the main thread and say thread B(created by main). 假设有两个线程,主线程和线程B(由main创建)。 If B acquired a mutex(say pthread_mutex) and it has called pthread_exit without unlocking the lock. 如果B获得了一个互斥锁(比如pthread_mutex)并且在没有解锁锁的情况下调用了pthread_exit。 So what happens to the mutex? 那么互斥体会发生什么? Does it become free? 它变得免费吗?

nope. 不。 The mutex remaines locked. 互斥锁保持锁定状态。 What actually happens to such a lock depends on its type, You can read about that here or here 这种锁实际发生了什么取决于它的类型,你可以在这里这里阅读

If you created a robust mutex by setting up the right attributes before calling pthread_mutex_init , the mutex will enter a special state when the thread that holds the lock terminates, and the next thread to attempt to acquire the mutex will obtain an error of EOWNERDEAD . 如果通过在调用pthread_mutex_init之前设置正确的属性来创建健壮的互斥锁,则当持有锁的线程终止时,互斥锁将进入特殊状态,并且尝试获取互斥锁的下一个线程将获得EOWNERDEAD的错误。 It is then responsible for cleaning up whatever state the mutex protects and calling pthread_mutex_consistent to make the mutex usable again, or calling pthread_mutex_unlock (which will make the mutex permanently unusable; further attempts to use it will return ENOTRECOVERABLE ). 然后它负责清除互斥锁保护的任何状态,并调用pthread_mutex_consistent以使互斥锁再次可用,或调用pthread_mutex_unlock (这将使互斥锁永久不可用;进一步尝试使用它将返回ENOTRECOVERABLE )。

For non-robust mutexes, the mutex is permanently unusable if the thread that locked it terminates without unlocking it. 对于非健壮的互斥锁,如果锁定它的线程终止而未解锁,则互斥锁将永久不可用。 Per the standard (see the resolution to issue 755 on the Austin Group tracker), the mutex remains locked and its formal ownership continues to belong to the thread that exited, and any thread that attempts to lock it will deadlock. 根据标准(参见在Austin Group跟踪器上发布755的决议),互斥锁保持锁定状态,其正式所有权继续属于退出的线程,并且任何试图锁定它的线程都将死锁。 If another thread attempts to unlock it, that's normally undefined behavior, unless the mutex was created with the PTHREAD_MUTEX_ERRORCHECK attribute, in which case an error will be returned. 如果另一个线程试图解锁它,那通常是未定义的行为,除非使用PTHREAD_MUTEX_ERRORCHECK属性创建互斥锁,在这种情况下将返回错误。

On the other hand, many (most?) real-world implementations don't actually follow the requirements of the standard. 另一方面,许多(大多数?)现实世界的实现实际上并不符合标准的要求。 An attempt to lock or unlock the mutex from another thread might spuriously succeed, since the thread id (used to track ownership) might have been reused and may now refer to a different thread (possibly the one making the new lock/unlock request). 尝试从另一个线程锁定或解锁互斥锁可能会虚假成功,因为线程ID(用于跟踪所有权)可能已被重用,现在可能引用另一个线程(可能是发出新锁定/解锁请求的线程)。 At least glibc's NPTL is known to exhibit this behavior. 至少glibc的NPTL已经表现出这种行为。

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

相关问题 当线程退出时,线程中声明的变量会发生什么? - What happens to variables declared in a thread when the thread exits? 当线程在已解锁的互斥锁上调用 pthread_mutex_unlock 时会发生什么 - What happens when a thread calls pthread_mutex_unlock on an already unlocked mutex pthread退出函数时会发生什么 - What happens to the pthread when it exits the function 如果在调用pthread_mutex_lock()时线程未成功执行该怎么办? - What happens if a thread doesn't succeed when making a pthread_mutex_lock() call? 当线程分叉时会发生什么? - What happens when a thread forks? 当pthread在等待互斥锁时死机会发生什么? - What happens when a pthread dies while waiting on a mutex? 当两个或多个哲学家检查互斥锁为 1 并同时检查互斥锁并输入测试函数时会发生什么 - What happens when two or more philosophers check the mutex to be 1 and at the same time down the mutex and enter test function 当线程在C中调用longjmp()时会发生什么 - What happens when a thread calls longjmp() in c 被 pthread_cond_signal() 唤醒但失去互斥锁竞争的线程会发生什么 - What happens to a thread that got woken up by pthread_cond_signal() but lost competition for a mutex 如何检查是否未获取互斥锁? - How to check if a mutex is not acquired?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM