This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4608. sleep_for is not an observable behavior

Section: 32.4.5 [thread.thread.this] Status: New Submitter: jim x Opened: 2026-07-24 Last modified: 2026-07-26

Priority: Not Prioritized

View all other issues in [thread.thread.this].

View all issues with New status.

Discussion:

Consider this example:

#include <iostream>
#include <thread>
#include <chrono>

int main() {
  std::cout << "start to sleep\n";
  std::this_thread::sleep_for(std::chrono::milliseconds(50000)); 
  std::cout << "continue after 50000ms\n";
  return 0;
}

According to 4.1.2 [intro.abstract] p8

The following specify the observable behavior of the program:

The specification of sleep_for says:

Effects: Blocks the calling thread for the relative timeout (32.2.4 [thread.req.timing]) specified by rel_time.

This description of its effect doesn't fall under any of the observable behavior bullets. Does it mean, according to the "as-if" rule, that a conforming implementation can arbitrarily eliminate the call to sleep_for? However, this doesn't align with the intent of providing this function in the standard library.

This issue resembles the need to promote "std::filesystem operations" to be observable behaviors, as appealed in LWG 3669(i). Guaranteed by implementations but not an observable behavior in the abstract machine can result in a conforming implementation eliminating the effect of sleep_for, which would make the program terminate quite early, merely holding the difference between two steady_clock::now as if sleep_for were not eliminated.

Proposed resolution: