It was already asked few times about whether one should notify condition_variable
while holding a lock:
lk.lock();
// change state
cv.notify_one();
lk.unlock();
or after releasing it:
lk.lock();
// change state
lk.unlock();
cv.notify_one();
Here's one of such questions: Do I have to acquire lock before calling condition_variable.notify_one()?
Answers point out that both are safe, as long as the lock is held at all when condition is changed, and the later form is better for performance: awaken thread is immediately unlocked.
I'm wondering how much this performance effect is generic and significant:
- Does this apply to Windows
CONDITION_VARIABLE
(that is avaliable since Vista+, but I'm nostly asking about Windows 10 implementation)? If so, how much significant the effect is? - Does this apply to Qt
QWaitCondition
on Windows? - Does this apply to
boost::condition_variable
on Windows? - Does it apply to most Linux systems where POSIX condition variable is the underlying implementation? Is there a measure of it?
The practical application is that I have to use the first form, where notification is done under the lock, as a structure that manages condition variables needs protection as well, so I want to know how much this approach worth avoiding.