Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX thread exit/crash/exception-crash while holding mutex

Is there a well defined behavior for POSIX mutex ownership in case of

  1. Thread exits
  2. Thread crashes
  3. Thread crashes due to exception

Suppose thread-1 owns a mutex. And thread-2 is waiting to acquire the same mutex. And thread-1 goes the 1/2/3 scenario. What is the effect on thread-2 ?

PS : I believe the behavior for spin-lock is, NOT to unblock thread-2, with reasoning that the section protected by spin-lock is in bad shape anyways.

like image 588
Ajeet Ganga Avatar asked Sep 08 '11 13:09

Ajeet Ganga


2 Answers

If you're worried about these issues, Robust Mutexes may be the tool you're looking for:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_setrobust.html

After a thread that owns a robust mutex terminates without unlocking it, the next thread that attempts to lock it will get EOWNERDEAD and become the new owner. This signals that it's responsible for cleaning up the state the mutex protects, and marking it consistent again with the pthread_mutex_consistent function before unlocking it. Unlocking it without marking it consistent puts the mutex in a permanently unrecoverable state.

Note that with robust mutexes, all code that locks the mutex must be aware of the possibility that EOWNERDEAD could be returned.

like image 71
R.. GitHub STOP HELPING ICE Avatar answered Oct 18 '22 12:10

R.. GitHub STOP HELPING ICE


It's really simple. If you don't explicitly unlock the mutex, it remains locked, regardless of what happened or why. This is c, not ruby on rails or visual basic.

like image 1
Kevin Avatar answered Oct 18 '22 13:10

Kevin