From 8d8a553f5bdd655b274fe940e02e0eca60324652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 15 Apr 2022 18:30:21 +0200 Subject: [PATCH] Fix typo and optimize locking pattern in Event, make it the same as LimitedWaitable. --- Common/Thread/Event.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Common/Thread/Event.h b/Common/Thread/Event.h index 3cc773da72..f675cd07d4 100644 --- a/Common/Thread/Event.h +++ b/Common/Thread/Event.h @@ -11,15 +11,21 @@ public: triggered_ = false; } + ~Event() { + // Make sure no one is still waiting, and any notify lock is released. + Notify(); + } + void Wait() override { - std::unique_lock lock; - if (!triggered_) { - cond_.wait(lock, [&] { return triggered_.load(); }); + if (triggered_) { + return; } + std::unique_lock lock(mutex_); + cond_.wait(lock, [&] { return triggered_.load(); }); } void Notify() { - std::unique_lock lock; + std::unique_lock lock(mutex_); triggered_ = true; cond_.notify_one(); }