0

I have checked that my system is supporting microsec precision how to make system support nanosec precision Using boost timer I am able to print the current time in microsec precision Print current time in microsec

Now when I check whether nanosleep / usleep is working or not, I run this program .

Here is my program1

Print current time in microsec 
usleep(1); // sleep for 1 microsec
Print current time in microsec 

Here is my output

01:43:38.799150
01:43:38.799393

When I run this program2

struct timespec delay;
delay.tv_sec = 0;
delay.tv_nsec = 1000;

Print current time in microsec 
nanosleep(&delay, NULL);
Print current time in microsec 

I got this output

01:48:25.919269
01:48:25.919838

I am expecting only 1 microsec difference in current time whereas actually I am getting more than 100 microsec difference. What's the reason?

I doubt whether my system properly execute usleep/nanosleep although usleep(1) also call indirectly nanosleep(1000)

I am using g++, linux. Any help or insight will be great and thanks in advance .

Community
  • 1
  • 1
bholanath
  • 1,699
  • 1
  • 22
  • 40
  • 2
    Sleeping in any modern multi-tasking operating system is not precise. You're guaranteed that the sleep will be *at least* the amount of time you requested, but that's it. You also have to take into consideration the time it takes to get and print the timestamps. – Some programmer dude Apr 08 '14 at 08:23
  • @JoachimPileborg Thanks for your comment. I have checked other option- not printing immediately, instead redirect to a stream and later print it. like std::ostringstream out; out << time_now(); usleep(1); out << time_now(); std::cout< – bholanath Apr 08 '14 at 09:17

0 Answers0