thread/*Cond: add wait_for() overload with predicate

This commit is contained in:
Max Kellermann
2019-05-07 19:54:54 +02:00
parent ad4ca0c449
commit 9095167039
3 changed files with 27 additions and 5 deletions

View File

@@ -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

View File

@@ -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