简体   繁体   中英

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.

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 eg 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM