File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -38,19 +38,34 @@ class WaitStrategy {
3838class 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
5671class SleepWaitStrategy : public WaitStrategy {
You can’t perform that action at this time.
0 commit comments