Skip to content

Commit 4bc83a2

Browse files
committed
Commentary of condition_variable::wait code
Signed-off-by: Bartłomiej Chmiel <bchmiel@antmicro.com>
1 parent 1f1ab2b commit 4bc83a2

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/V3ThreadPool.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ void V3ThreadPool::workerJobLoop() {
6565
while (true) {
6666
std::function<void()> job;
6767
{
68+
// Locking before `condition_variable::wait` is required to ensure that the
69+
// `m_cv` condition will be executed under a lock. Taking a lock
70+
// before `condition_variable::wait` may lead to missed
71+
// `condition_variable::notify_all` notification (when a thread waits at the lock
72+
// before) but, according to C++ standard, the `condition_variable::wait` first checks
73+
// the condition and then waits for the notification, thus even when notification is
74+
// missed, the condition still will be checked.
6875
const V3LockGuard lock{m_mutex};
6976
m_cv.wait(m_mutex,
7077
[&]() VL_REQUIRES(m_mutex) { return !m_queue.empty() || m_shutdown; });

0 commit comments

Comments
 (0)