Skip to content
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