After answering to a different question, I decided to investigate the exact issue behind spurious wakes.
First and foremost: This is NO (!!!) dulicate of any question here. Most questions just ask how to properly handle spurious wakes, but none of them actually tries to break down HOW they happen.
We all know that a wait() on a condition_variable may cause a wake up, even though notify() is not called.
std::mutex cond_mutex;
std::condition_variable cv;
void test(){
std::unique_lock<std::mutex> lk(cond_mutex);
cv.wait(lk); // may wake up without notify()
}
I know how to properly wait(), but what I do not know is what exactly causes these spurious wakes.
Would be interesting if someone could shed some light on it, from a very low level perspective. It must be some kind of race condition, but I would like to understand exactly how / why it occurs.