Skip to content

Commit 43823c6

Browse files
superMan-1012ccr
authored andcommitted
fix: event lost
1 parent 38ae485 commit 43823c6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

cyber/base/wait_strategy.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,34 @@ class WaitStrategy {
3838
class BlockWaitStrategy : public WaitStrategy {
3939
public:
4040
BlockWaitStrategy() {}
41-
void NotifyOne() override { cv_.notify_one(); }
41+
void NotifyOne() override {
42+
{
43+
std::lock_guard<std::mutex> lock(mutex_); // 锁定互斥锁
44+
notify_one_ = true;
45+
}
46+
cv_.notify_one();
47+
}
4248

4349
bool EmptyWait() override {
4450
std::unique_lock<std::mutex> lock(mutex_);
45-
cv_.wait(lock);
51+
cv_.wait(lock, [this] { return break_all_wait_ || notify_one_; });
52+
notify_one_ = false;
4653
return true;
4754
}
4855

49-
void BreakAllWait() override { cv_.notify_all(); }
56+
void BreakAllWait() override {
57+
{
58+
std::lock_guard<std::mutex> lock(mutex_); // 锁定互斥锁
59+
break_all_wait_ = true;
60+
}
61+
cv_.notify_all();
62+
}
5063

5164
private:
5265
std::mutex mutex_;
5366
std::condition_variable cv_;
67+
bool notify_one_{false};
68+
bool break_all_wait_{false};
5469
};
5570

5671
class SleepWaitStrategy : public WaitStrategy {

0 commit comments

Comments
 (0)