A spurious wakeup occurs when a condition-variable wait reacquires its mutex and checks the condition without a notification causing that check. A wake is not proof that the awaited state is true: keep checking the condition, preferably with the predicate form of wait. The predicate may be checked repeatedly while the mutex is locked, so avoid side effects in it. section 4.1.1
For timed waits, repeated relative timeouts can restart the full wait after each spurious wake and make the total wait unbounded. Use one fixed deadline and check the condition in a loop. section 4.3.3
template<typename Predicate>
void minimal_wait(std::unique_lock<std::mutex>& lk,Predicate pred){
while(!pred()){
lk.unlock();
lk.lock();
}
}
A minimal wait implementation loops until the predicate is true, unlocking and relocking around each check. section 4.1.1
