thread/*Cond: add wait_for() overload with predicate
This commit is contained in:
@@ -60,11 +60,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
bool LockWaitFinished() noexcept {
|
bool LockWaitFinished() noexcept {
|
||||||
std::unique_lock<Mutex> lock(mutex);
|
std::unique_lock<Mutex> lock(mutex);
|
||||||
while (!finished)
|
return cond.wait_for(lock, timeout, [this]{ return finished; });
|
||||||
if (!cond.wait_for(lock, timeout))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -105,6 +105,19 @@ public:
|
|||||||
|
|
||||||
return wait_for(lock, timeout_us);
|
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
|
#endif
|
||||||
|
@@ -70,6 +70,19 @@ public:
|
|||||||
&lock.mutex()->critical_section,
|
&lock.mutex()->critical_section,
|
||||||
timeout_ms);
|
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
|
#endif
|
||||||
|
Reference in New Issue
Block a user