Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). If B acquired a mutex(say pthread_mutex) and it has called pthread_exit without unlocking the lock. So what happens to the mutex? Does it become free?

like image 890
Sadish Avatar asked Dec 12 '10 21:12

Sadish


People also ask

Does mutex need to be destroyed?

Like any system resource that can be shared among threads, a mutex allocated on a thread's stack must be destroyed before the thread is terminated) actually does explicitly state this in their documentation.

What happens when mutex is locked?

Mutexes are used to protect shared resources. If the mutex is already locked by another thread, the thread waits for the mutex to become available. The thread that has locked a mutex becomes its current owner and remains the owner until the same thread has unlocked it.

What happens if mutex is not released?

The mutex object is destroyed when its last handle has been closed. It makes a loud bang.

What happens when mutex lock is used more than once?

When this kind of mutex is locked multiple times by the same thread, then unlock will decrement the count and no waiting thread is posted to continue running with the lock. If the count is decremented to zero, then the mutex is released and if any thread is waiting for it is posted.


1 Answers

nope. The mutex remaines locked. What actually happens to such a lock depends on its type, You can read about that here or here

like image 80
SingleNegationElimination Avatar answered Nov 16 '22 03:11

SingleNegationElimination