5

I have a portion of code, where one thread calls something like:

cond->notify_all();
delete cond;

with

std::condition_variable_any cond;

Afaik, this should work, since I should be allowed to delete the condition variable, as soon as I notified all threads waiting on it, they don't have to have resumed from the wait call.

On Windows, this sometimes, crashes for me with an error:

mutex destroyed while busy

printed to stdout

On linux, with clang 3.5 this works perfectly fine, on windows I use Visual Studio 2013, with v120 tookit, v120 is the default one.

Did I do something wrong, did I missunderstand the standard, or is M$ doing something wrong here? If it does, how can I work around that?

T.C.
  • 133,968
  • 17
  • 288
  • 421
Ongy
  • 271
  • 3
  • 13

1 Answers1

4

Microsoft's implementation of std::condition_variable_any is non-conforming. According to C++14 STL Features, Fixes, And Breaking Changes In Visual Studio 14 CTP1 this has been fixed in the not-yet-released 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).

Casey
  • 41,449
  • 7
  • 95
  • 125