Passing a dummy lock to std::condition_variable_any::wait
You are working on a false premise.
The mutex does not only protect the condition predicate, it also protects the condition_variable itself.
So the mutex should be at the same scope as the condition_variable and all locks should lock that same mutex.
like this:
// global scopestd::mutex mut;std::condition_variable cond_var;// thread scope{ std::unique_lock<std::mutex> lock(mut); cond_var.wait(lock);}
see here: Why do pthreads’ condition variable functions require a mutex?
What you're suggesting would be undefined behavior. The section on condition_variable_any
leads with, emphasis mine:
A
Lock
type shall meet theBasicLockable
requirements (30.2.5.2). [ Note: All of the standard mutex types meet this requirement. If a Lock type other than one of the standard mutex types or a unique_lock wrapper for a standard mutex type is used with condition_variable_any, the user must ensure that any necessary synchronization is in place with respect to the predicate associated with thecondition_variable_any
instance. —end note ]
The BasicLockable
requirements themselves don't just describe the interface, they also describe the required semantics:
A type
L
meets theBasicLockable
requirements if the following expressions are well-formed and have the specified semantics (m denotes a value of type L).
m.lock()
2 Effects: Blocks until a lock can be acquired for the current execution agent. If an exception is thrown then a lock shall not have been acquired for the current execution agent.
If your dummy_lock
doesn't actually acquire a lock, it isn't a BasicLockable
, and so you fail to meet the premise for condition_variable_any
. At that point, all bets are off, and you cannot expect wait()
to do anything reasonable.