std::condition_variable::wait_for uses SleepConditionVariableSRW
on Windows, which has millisecond precision.
In order to avoid busy loops, we won't wait for less than one
millisecond on Windows.
Note that this situation is quite common since on Windows,
"wait_for" often returns ~1ms before the specified timeout.
Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
auto p = schedule.begin();
// is the future now?
- if (p->first > now)
- break;
+ #if defined(_WIN32)
+ if (p->first - now > std::chrono::milliseconds(1)) {
+ // std::condition_variable::wait_for uses SleepConditionVariableSRW
+ // on Windows, which has millisecond precision. Deltas <1ms will
+ // lead to busy loops, which should be avoided. This situation is
+ // quite common since "wait_for" often returns ~1ms earlier than
+ // requested.
+ break;
+ }
+ #else // !_WIN32
+ if (p->first > now) {
+ break;
+ }
+ #endif
Context *callback = p->second;
events.erase(callback);
while (!schedule.empty()) {
auto p = schedule.begin();
// Should we wait for the future?
- if (p->t > now)
- break;
+ #if defined(_WIN32)
+ if (p->t - now > std::chrono::milliseconds(1)) {
+ // std::condition_variable::wait_for uses SleepConditionVariableSRW
+ // on Windows, which has millisecond precision. Deltas <1ms will
+ // lead to busy loops, which should be avoided. This situation is
+ // quite common since "wait_for" often returns ~1ms earlier than
+ // requested.
+ break;
+ }
+ #else // !_WIN32
+ if (p->t > now) {
+ break;
+ }
+ #endif
auto& e = *p;
schedule.erase(e);