I have what I thought was a simple pattern - I want to make a timepoint 5 seconds in the future, run a task which might take a while, and then sleep until that timepoint (potentially not sleeping at all if that time has already been reached.) However, whenever trying to use std::this_thread::sleep_until
with a timepoint in the past, my application instead hangs forever. Here is an MCVE:
#include <chrono>
#include <thread>
int main(){
std::this_thread::sleep_until(std::chrono::steady_clock::now() - std::chrono::seconds(1));
}
Using g++ (GCC) 4.8.5, this never returns. I've also tried system_clock with the same results. Using strace to examine what's happening, the last thing I get is:
nanosleep({4294967295, 0},
so I guess it would return eventually, but I don't feel like waiting that long.
Is this a g++ bug? I can't imagine this behavior is intentional. I found the question Is behaviour well-defined when sleep_until()
specifies a time point in the past? but it doesn't seem that any conclusion was actually reached on whether or not the standard actually specifies what should happen. I've since implemented another solution to my problem; I'm just curious as to whether or not what I'm seeing is UB or a bug.