Boost :: thread_sleep vs boost :: chrono :: thread_clock inconsistent

When i run this code

#include <iostream>
#include <boost/thread.hpp>
#include <boost/chrono/thread_clock.hpp>

void foo() {
    boost::this_thread::sleep(boost::posix_time::microseconds(500));
}


int main() {
    boost::chrono::thread_clock::time_point start = boost::chrono::thread_clock::now();    
    foo();   
    boost::chrono::thread_clock::time_point stop = boost::chrono::thread_clock::now();    
    std::cout << "duration = " 
              << boost::chrono::duration_cast<boost::chrono::microseconds>(stop-start).count() 
              << " microsec\n";
}

I get the following output:

duration = 121 microsec
duration = 121 microsec
duration = 110 microsec
duration = 114 microsec

Also, when I replace 500with another value, for example 200, the output is always around 100. Compiling with -O3. Why is the time not agreed?

PS: I read that I’m sleepout of date, but when I replace it with

boost::this_thread::sleep_for(boost::chrono::microseconds(200));

the output is in the range of ~ 20 microseconds.

+4
source share
1 answer

Indeed, I did not notice anything obvious. The documentation says:

the thread_clock class provides access to real threading hours, i.e. real clock CPU time of the calling thread.

, - , , , . , , boost::chrono::system_clock.

0

Source: https://habr.com/ru/post/1682316/


All Articles