gpt4 book ai didi

c++ - g++ 中的递归 lock() 调用

转载 作者:搜寻专家 更新时间:2023-10-31 00:29:50 24 4
gpt4 key购买 nike

为什么下面的代码会导致 g++ 5.4.0 中的死锁?

#include <mutex>

int main()
{
std::mutex m;
m.lock();
m.lock();
}

据我所知,根据标准,这段代码应该会导致异常:

30.4.1.2 Mutex types [thread.mutex.requirements.mutex]

6 The expression m.lock() shall be well-formed and have the following semantics:

[...]

12 Throws: system_error when an exception is required (30.2.2).

13 Error conditions:

(13.1) — operation_not_permitted — if the thread does not have the privilege to perform the operation.

(13.2) — resource_deadlock_would_occur — if the implementation detects that a deadlock would occur.

(13.3) — device_or_resource_busy — if the mutex is already locked and blocking is not possible.

那怎么了?是库中的错误吗?

最佳答案

你的代码有未定义的行为,因为你违反了先决条件([thread.mutex.requirements.mutex]):

The expression m.lock() shall be well-formed and have the following semantics:

Requires: If m is of type std::mutex [...], the calling thread does not own the mutex.

关于c++ - g++ 中的递归 lock() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39353607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com