0

Consider following example, On windows 7 icore7 laptop(VC++2010) and ubuntu 64bit 12.04 lte gcc 4.6.3

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
typedef boost::posix_time::ptime Time;
typedef boost::posix_time::time_duration TimeDuration;
int main()
{
    Time t1;
    Time t2;
    TimeDuration dt;
    boost::posix_time::microseconds tosleep=boost::posix_time::microseconds(100);
    for(int i=0;i<10;i++){

        t1=boost::posix_time::microsec_clock::local_time();
        //std::cout <<i << std::endl; // on win7 without this all outputs are 0
        boost::this_thread::sleep( tosleep );

        t2=boost::posix_time::microsec_clock::local_time();

        dt = t2 - t1;
        long long msec = dt.total_microseconds();
        std::cout << msec << std::endl;
    }
    return 0;
}

I was expecting that my thread will sleep constantly 100 microsecond, but output is something weird :

=========== AND OUTPUT ==================
arm2arm@cudastartub:~$ g++ -O3 sleepme.cpp -lboost_thread
arm2arm@cudastartub:~$ ./a.out
726
346
312
311
513
327
394
311
306
445

Is boost has a some overhead? What I need for RealTime systems, where microseconds are important?

Arman
  • 4,566
  • 10
  • 45
  • 66

1 Answers1

0

On Windows one can't Sleep() less than 1 ms, so your sleep(tosleep) call is equivalent to sleep(0). (See also this link)

Of course, std::cout <<i << std::endl would take some time...

Community
  • 1
  • 1
Igor R.
  • 14,716
  • 2
  • 49
  • 83