If you unlock an already unlocked mutex, is the behavior undefined?

前端 未结 6 1661
半阙折子戏
半阙折子戏 2020-12-29 03:10

If you unlock an already unlocked mutex, is the behavior unsafe, safe, or undefined?

The purpose of the question is related to the following code, where I don\'t kno

6条回答
  •  醉酒成梦
    2020-12-29 03:56

    A mutex unlock should be done in a thread only if the same mutex is locked earlier in the same thread. All other cases are undefined behviour as per man page.

    If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in undefined behaviour. Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behaviour. Attempting to unlock the mutex if it is not locked results in undefined behaviour.

提交回复
热议问题