Why is locking a std::mutex twice 'Undefined Behaviour'?

后端 未结 2 888
孤街浪徒
孤街浪徒 2020-12-02 01:25

As per this article:

If you try and lock a non-recursive mutex twice from the same thread without unlocking in between, you get undefined behavior.

2条回答
  •  自闭症患者
    2020-12-02 02:11

    Undefined behavior allows implementations to do whatever is fastest/most convenient. For example, an efficient implementation of a non-recursive mutex might be a single bit where the lock operation is implemented with an atomic compare-and-swap instruction in a loop. If the thread that owns the mutex tries to lock it again it will deadlock because it is waiting for the mutex to unlock but since nobody else can unlock it (unless there's some other bug where some thread that doesn't own it unlocks it) the thread will wait forever.

提交回复
热议问题