Here is some piece of code that will be called from multiple threads. Predicate for std::condition_variable::wait will be a little different for each thread but it doesn't change the problem.
std::unique_lock<std::mutex> lock{connection_mutex};
cv.wait(lock,
[conn = shared_from_this()]
{
return conn->connection_is_made();
}
);
//do some stuff
lock.unlock();
I read this at cppreference
When the condition variable is notified, a timeout expires, or a spurious wakeup occurs, the thread is awakened, and the mutex is atomically reacquired. The thread should then check the condition and resume waiting if the wake up was spurious.
From this I understand that lock will be locked, than a predicate lambda will be checked, and if it returns true the lock will remain locked and I can continue to do some stuff under the protection of this mutex. When std::condition_variable is notified by notify_one() member function it makes perfect sense.
But what happens when std::condition_variable is notified by notify_all()? I cant find in documentation if all the threads are woken up and than "waits in a line" to lock a mutex and only then check what the predicate returns or maybe they do something else.
Aucun commentaire:
Enregistrer un commentaire