What happens if Non Recursive mutex is locked more than once?


  • 0

    Deadlock!

    If a thread which had already locked a mutex, tries to lock the mutex again, it will enter into waiting list of that mutex which results in deadlock.


  • 0
    J

    @mehtapaxshal hey, you might be right about the deadlock, but from what I understand the only way it would result in a deadlock is if process 2 was holding a different lock that process 1 needed in order to finish and neither process can force the other to give up a lock. Otherwise I think process 1 could finish and process 2 would run, but maybe I am missing something.


  • 0
    F

    This post is talking a bout "Non Recursive" mutex. From wikipedia(https://en.wikipedia.org/wiki/Reentrant_mutex):

    any attempt to perform the "lock" operation on an ordinary mutex (lock) would either fail or block when the mutex is already locked.

    Then here comes "Recursive"(Reentrant) mutex, which could be locked multiple times by a thread, also requires same amount of unlock operations.

    Therefore, if it is an ordinary mutex(non-reentrant), a thread that locks it more than once will either fail or locked(deadlock, cause the thread is waiting itself to unlock the mutex).


  • 0

    @feng.wen.503 True that! Thats make Non Recursive mutex different from Reentrant mutex.

    @JNX Hope you got that! Your understanding is also right but its different scenario. I am here talking about deadlock with single or same thread/process.


  • 0
    S

    Actually, the behavior is undefined. Technically, its not a deadlock.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.