gpt4 book ai didi

c++ - 如果 unique_lock 管理的互斥量被销毁怎么办?

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:10 25 4
gpt4 key购买 nike

void Fn() {
std::mutex* mutex = new std::mutex;
std::unique_lock<std::mutex> lock(*mutex);
delete mutex;
}

上面的代码片段中是否有未定义的行为?

最佳答案

甚至在锁的析构函数运行之前,您就处于 UB 领域。 §30.4.1.2.1 [thread.mutex.class]/p5:

The behavior of a program is undefined if it destroys a mutex object owned by any thread or a thread terminates while owning a mutex object.

关于c++ - 如果 unique_lock 管理的互斥量被销毁怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495413/

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