Can std::this_thread::sleep_for() have spurious wakeups?
The relevant sections of the C++ Standard (paragraphs [thread.thread.this]/7-9) do not mention anything about spurious wake-ups for std::this_thread::sleep_for
, unlike e.g. for std::condition_variable::wait_for
.
template <class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time);
7 Effects: Blocks the calling thread for the relative timeout (30.2.4) specified by
rel_time
.8 Synchronization: None.
9 Throws: Timeout-related exceptions (30.2.4).
This implies that the behavior you are observing is non-conforming.
Able to reproduce on GCC 4.8.5.
There is also a bug reported (and confirmed) against GCC 5.1.0 (gcc.gnu.org/bugzilla/show_bug.cgi?id=66803)