https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66803
Bug ID: 66803 Summary: std::this_thread::sleep_for gets interrupted by signals. Product: gcc Version: 5.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: adam at mizerski dot pl Target Milestone: --- Created attachment 35930 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35930&action=edit preprocessed source #include <chrono> #include <iostream> #include <thread> #include <csignal> void signal_handler(int) {} int main() { using namespace std::chrono_literals; std::signal(SIGUSR1, signal_handler); const auto start = std::chrono::high_resolution_clock::now(); std::this_thread::sleep_for(5s); const auto end = std::chrono::high_resolution_clock::now(); std::cout << "Waited " << std::chrono::duration_cast<std::chrono::seconds>(end-start).count() << " s\n"; return 0; } Compilation command: g++-5.1 -save-temps -std=c++14 test_cpp.cpp -o test_cpp -O2 -Wall -Wextra -pedantic -pthread Preprocessed source in attachment. Testing command line: ./test_cpp & sleep 2 && kill -USR1 $! Expected result: std::this_thread::sleep_for(5s); sleeps for 5 seconds. Actual result: std::this_thread::sleep_for(5s); sleeps for 2 seconds. The behavior of std::this_thread::sleep_for seems similar to sleep from unistd.h - it gets interrupted by signal handler. But std::this_thread::sleep_for unlike sleep has no way to report if it was interrupted or time left to sleep. C++14 standard (N3797) in 30.3.2 says that sleep_for should sleep specified amount of time and nothing about any interrupts. The same source compiled with "clang++ -std=c++14 -stdlib=libc++ -lc++abi test_cpp.cpp -o test_cpp -O2" (with clang version 3.5.0) behaves as expected. $ g++-5.1 -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/5.1.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /usr/src/gcc/configure --disable-multilib --enable-languages=c,c++ Thread model: posix gcc version 5.1.0 (GCC)