thread/*Cond: add wait_for() overload with predicate
This commit is contained in:
parent
ad4ca0c449
commit
9095167039
@ -60,11 +60,7 @@ public:
|
||||
private:
|
||||
bool LockWaitFinished() noexcept {
|
||||
std::unique_lock<Mutex> lock(mutex);
|
||||
while (!finished)
|
||||
if (!cond.wait_for(lock, timeout))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return cond.wait_for(lock, timeout, [this]{ return finished; });
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,6 +105,19 @@ public:
|
||||
|
||||
return wait_for(lock, timeout_us);
|
||||
}
|
||||
|
||||
template<typename M, typename P>
|
||||
bool wait_for(std::unique_lock<M> &lock,
|
||||
std::chrono::steady_clock::duration timeout,
|
||||
P &&predicate) noexcept {
|
||||
while (!predicate()) {
|
||||
// TODO: without wait_until(), this multiplies the timeout
|
||||
if (!wait_for(lock, timeout))
|
||||
return predicate();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -70,6 +70,19 @@ public:
|
||||
&lock.mutex()->critical_section,
|
||||
timeout_ms);
|
||||
}
|
||||
|
||||
template<typename M, typename P>
|
||||
bool wait_for(std::unique_lock<M> &lock,
|
||||
std::chrono::steady_clock::duration timeout,
|
||||
P &&predicate) noexcept {
|
||||
while (!predicate()) {
|
||||
// TODO: without wait_until(), this multiplies the timeout
|
||||
if (!wait_for(lock, timeout))
|
||||
return predicate();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user