From 43823c67c1c5812bb816f2e8909dd598d175a90a Mon Sep 17 00:00:00 2001 From: ccr <735819385@qq.com> Date: Wed, 8 Jan 2025 17:37:10 +0800 Subject: [PATCH] fix: event lost --- cyber/base/wait_strategy.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cyber/base/wait_strategy.h b/cyber/base/wait_strategy.h index 2e9b292b9fd..678c98bc8a8 100644 --- a/cyber/base/wait_strategy.h +++ b/cyber/base/wait_strategy.h @@ -38,19 +38,34 @@ class WaitStrategy { class BlockWaitStrategy : public WaitStrategy { public: BlockWaitStrategy() {} - void NotifyOne() override { cv_.notify_one(); } + void NotifyOne() override { + { + std::lock_guard lock(mutex_); // 锁定互斥锁 + notify_one_ = true; + } + cv_.notify_one(); + } bool EmptyWait() override { std::unique_lock lock(mutex_); - cv_.wait(lock); + cv_.wait(lock, [this] { return break_all_wait_ || notify_one_; }); + notify_one_ = false; return true; } - void BreakAllWait() override { cv_.notify_all(); } + void BreakAllWait() override { + { + std::lock_guard lock(mutex_); // 锁定互斥锁 + break_all_wait_ = true; + } + cv_.notify_all(); + } private: std::mutex mutex_; std::condition_variable cv_; + bool notify_one_{false}; + bool break_all_wait_{false}; }; class SleepWaitStrategy : public WaitStrategy {