Pthread Mutex lock unlock by different threads

前端 未结 3 1034
既然无缘
既然无缘 2020-12-02 23:47

A Naive question ..

I read before saying - \"A MUTEX has to be unlocked only by the thread that locked it.\"

But I have written a program wh

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 00:41

    What you've done is simply not legal, and the behavior is undefined. Mutexes only exclude threads that play by the rules. If you tried to lock mutex1 from thread 2, the thread would be blocked, of course; that's the required thing to do. There's nothing in the spec that says what happens if you try to unlock a mutex you don't own!

提交回复
热议问题