sleeper_thread: Make sleep_for more robust
In the previous fix for a passed max duration, the assumption was made that at maximum one second will pass between the duration assignment and the std::condition_variable::sleep_for() call. This implementation makes the behavior more predictable by using sleep_until() instead to emulate the sleep_for() behavior.
This commit is contained in:
parent
ad7d4eb07d
commit
521dac8086
|
@ -67,13 +67,13 @@ class SleeperThread {
|
||||||
auto sleep_for(std::chrono::system_clock::duration dur) {
|
auto sleep_for(std::chrono::system_clock::duration dur) {
|
||||||
std::unique_lock lk(mutex_);
|
std::unique_lock lk(mutex_);
|
||||||
CancellationGuard cancel_lock;
|
CancellationGuard cancel_lock;
|
||||||
using timepoint = std::chrono::system_clock::time_point;
|
constexpr auto max_time_point = std::chrono::steady_clock::time_point::max();
|
||||||
static_assert(std::numeric_limits<timepoint::rep>::max() >=
|
auto wait_end = max_time_point;
|
||||||
std::numeric_limits<decltype(dur)::rep>::max());
|
auto now = std::chrono::steady_clock::now();
|
||||||
if (std::chrono::system_clock::now() >= timepoint::max() - dur) {
|
if (now < max_time_point - dur) {
|
||||||
dur = timepoint::max() - std::chrono::system_clock::now() - std::chrono::seconds(1);
|
wait_end = now + dur;
|
||||||
}
|
}
|
||||||
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
|
return condvar_.wait_until(lk, wait_end, [this] { return signal_ || !do_run_; });
|
||||||
}
|
}
|
||||||
|
|
||||||
auto sleep_until(
|
auto sleep_until(
|
||||||
|
|
Loading…
Reference in New Issue