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

标签 c++ c++11 condition-variable

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

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/

相关文章:

c++ - VIM、自动格式化、代码指南、C++

c++ - 创建不同的 v8 上下文,它们是另一个的克隆

c++ - 为什么基类指针指向基类中的纯虚方法而不是派生类中的重写方法?

c++ - 为什么这个 Makefile 不能正确编译?

c++ - move 语义 std::move 如何使用它

C++14 值初始化问题

c++ - 为什么我不能从派生类中调用基类运算符?

multithreading - 什么时候需要条件变量,互斥锁还不够吗?

c++ - 条件变量基本示例

c++ - 在生产者-消费者情况下使用条件变量