gpt4 book ai didi

c++ - notify_all 崩溃后直接删除 std::condition_variable_any

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:09 26 4
gpt4 key购买 nike

我有一部分代码,其中一个线程调用如下内容:

cond->notify_all();
delete cond;

std::condition_variable_any cond;

Afaik,这应该有效,因为 I should be allowed to delete the condition variable, as soon as I notified all threads waiting on it ,他们不必从 wait 调用中恢复。

在 Windows 上,这有时会因错误而崩溃:

mutex destroyed while busy

打印到标准输出

在 Linux 上,使用 clang 3.5 这工作得很好,在 Windows 上我使用 Visual Studio 2013,v120 takeit,v120 是默认的。

我是不是做错了什么,是我误解了标准,还是 M$ 在这里做错了什么?如果是这样,我该如何解决?

最佳答案

Microsoft 的 std::condition_variable_any 实现不符合规范。根据C++14 STL Features, Fixes, And Breaking Changes In Visual Studio 14 CTP1这已在尚未发布的 VS14 中修复:

We've implemented the extremely subtle rule specified by 30.5.2 [thread.condition.condvarany]/5:

~condition_variable_any()

Requires: There shall be no thread blocked on *this. [ Note: That is, all threads shall have been notified; they may subsequently block on the lock specified in the wait. This relaxes the usual rules, which would have required all wait calls to happen before destruction. Only the notification to unblock the wait must happen before destruction. The user must take care to ensure that no threads wait on *this once the destructor has been started, especially when the waiting threads are calling the wait functions in a loop or using the overloads of wait, wait_for, or wait_until that take a predicate. -end note ]

(DevDiv#484720)。

关于c++ - notify_all 崩溃后直接删除 std::condition_variable_any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25796287/

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