You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CP: std::condition_variable(), unlock mutex before notfifying waiting thread(s)? #1272
void worker_thread()
{
:
:
// Manual unlocking is done before notifying, to avoid waking up
// the waiting thread only to block again (see notify_one for details)
lk.unlock();
cv.notify_one();
}
And the following note regarding notify_one() / notify_all():
The notifying thread does not need to hold the lock on the same mutex as
the one held by the waiting thread(s); in fact doing so is a
pessimization, since the notified thread would immediately block again,
waiting for the notifying thread to release the lock. However, some
implementations (in particular many implementations of pthreads)
recognize this situation and avoid this "hurry up and wait" scenario by
transferring the waiting thread from the condition variable's queue
directly to the queue of the mutex within the notify call, without
waking it up.
Question 1: Is this a good advice?
Question 2: Would it make sense to address it in the C++ Core Guidelines too?
It's reasonable advice for performance, but that's not the main focus of the guidelines here. It's a minor optimisation, but not necessary for correctness.
Yes, that's what Howard suggested, and he gave a (IMHO uncommon) example where it matters. And I objected to recommending it by default, as it's not part of the "how to use condvars correctly" advice I was proposing there.
So"unlock first" is advice that doesn't usually make any difference for performance, and sometimes is unsafe (if the mutex lock is needed to ensure the CV remains alive). So not a good guideline.
So"unlock first" is advice that doesn't usually make any difference for performance, and sometimes is unsafe (if the mutex lock is needed to ensure the CV remains alive).
Please @jwakely sorry to bring this up again after over 5 years but can you elaborate further on this? An instance or an example will help a lot.
The cases where the notifying thread needs to lock the mutex are:
to ensure another thread doesn't destroy the condvar before the notify, e.g. because some other thread sees that the condition has become true and deletes some object which contains the condvar. Holding the mutex while notifying can be used to prevent other heads seeing that the condition is true until after the notify has been sent.
to ensure that another thread won't check the condition when it's false, but then it becomes true and the notify is done, then the first thread waits on the condvar, but it already missed the notification. This often happens when using an atomic variable for the condition and people think that the atomic variable avoids any data races. The updates to the atomic variable might be safe, but there's still a chance that its value can change after you check it and before you start to sleep. Holding the mutex while notifying (and when checking the condition and waiting in another thread) ensures that the notification isn't missed.
Activity
jwakely commentedon Sep 24, 2018
It's reasonable advice for performance, but that's not the main focus of the guidelines here. It's a minor optimisation, but not necessary for correctness.
cubbimew commentedon Sep 24, 2018
Wasn't the plan to provide the opposite guideline in #554 (comment) ?
jwakely commentedon Sep 24, 2018
Yes, that's what Howard suggested, and he gave a (IMHO uncommon) example where it matters. And I objected to recommending it by default, as it's not part of the "how to use condvars correctly" advice I was proposing there.
So"unlock first" is advice that doesn't usually make any difference for performance, and sometimes is unsafe (if the mutex lock is needed to ensure the CV remains alive). So not a good guideline.
Fix a race condition in timer_queue.cc.
Chukwujiobi-Canon commentedon May 29, 2023
Please @jwakely sorry to bring this up again after over 5 years but can you elaborate further on this? An instance or an example will help a lot.
jwakely commentedon Jan 29, 2025
The cases where the notifying thread needs to lock the mutex are: