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

后端 未结 2 896
孤街浪徒
孤街浪徒 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:23

    Because it never happens in a correct program, and making a check for something that never happens is wasteful (and to make that check it needs to store the owning thread ID, which is also wasteful).

    Note that it being undefined allows debug implementations to throw an exception, for example, while still allowing release implementations to be as efficient as possible.

提交回复
热议问题