mercredi 8 juillet 2020

Is relation between this_thread::sleep_for and chrono clocks specified by C++11 standard?

Consider following code:

        using namespace std;
        using namespace std::chrono;
        typedef std::intmax_t intmax_type;

        steady_clock::time_point start = steady_clock::now();

        this_thread::sleep_for(chrono::milliseconds(25000));

        steady_clock::duration dur = 
            steady_clock::now() - start;

        intmax_type desired_dur = duration_cast<milliseconds>(dur).count();

        if(desired_dur < intmax_type(25000))
            std::cout << "WTF happend there?" << std::endl;

According to standard std::this_thread::sleep_for(sleep_duration) may block for longer than sleep_duration due to scheduling or resource contention delays but at least it blocks thread execution for the specified sleep_duration.

There may be situation when the thread actually sleeps for specified duration but due to std::chrono::steady_clock or std::chrono::system_clock using different OS clocks than sleep_for implementation (different granularity f.e.) time period measurement gives us different result than actual sleep time was.

My question is:

Is it prohibited by C++11 standard for condition if(desired_dur < intmax_type(25000)) to happen? If so provide the exact quotes please.

Aucun commentaire:

Enregistrer un commentaire