Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can std::this_thread::sleep_for() have spurious wakeups?

Note, this is not a question about std::condition_variable::wait_for(). I know that can wake spuriously.

My program’s behavior suggests the answer to this question is Yes, but the STL documentation is quite clear for the condition_variable case. At least at cppreference.com, the correct answer for this_thread appears to be No.

Compiler is gcc 4.8.1, in case this is a defect.

like image 925
Andrew Lazarus Avatar asked May 29 '15 20:05

Andrew Lazarus


2 Answers

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.

like image 75
Andy Prowl Avatar answered Oct 18 '22 06:10

Andy Prowl


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)

like image 44
Paolo Brandoli Avatar answered Oct 18 '22 04:10

Paolo Brandoli