Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enhance BlockWaitStrategy Synchronization with Flags and Predicate-Based Waiting #15632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions cyber/base/wait_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,34 @@ class WaitStrategy {
class BlockWaitStrategy : public WaitStrategy {
public:
BlockWaitStrategy() {}
void NotifyOne() override { cv_.notify_one(); }
void NotifyOne() override {
{
std::lock_guard<std::mutex> lock(mutex_); // 锁定互斥锁
notify_one_ = true;
}
cv_.notify_one();
}

bool EmptyWait() override {
std::unique_lock<std::mutex> 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<std::mutex> 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 {
Expand Down