Reputation: 143795
Suppose I have the following situation. I have a thread, and this thread is holding a lock. While it's doing so, I call pthread_create, so now I have two threads. Suppose the second thread gets to unlock the lock. What happens when the first thread hits the unlock?
Upvotes: 1
Views: 310
Reputation: 6092
A thread should only unlock a lock that it has locked itself, and thus your second thread should not attempt to unlock a mutex that has been locked by the first thread.
Trying to unlock a mutex that has been locked by another thread is undefined behavior, unless the mutex type is PTHREAD_MUTEX_ERRORCHECK
, in which case an error is returned.
More info here: http://linux.die.net/man/3/pthread_mutex_lock
Relevant section:
If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results.
If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking shall be provided. If a thread attempts to relock a mutex that it has already locked, an error shall be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error shall be returned.
Upvotes: 1