When I call sleep() and check gettimeofday before and after, it sleeps longer each time I call it. Does anyone know of a way to sleep for a set amount of real time?
When I call sleep() and check gettimeofday before and after, it sleeps longer each time I call it. Does anyone know of a way to sleep for a set amount of real time?
This is OS dependent. On Linux, you can use nanosleep() to get the job done. It has a bit more guarantees that sleep().
You can never get your OS to sleep for exact n seconds unless you have a real-time OS. You can come close, though.
A scheme is to time how long your sleeps take and adjust the following sleeps in order to achieve "semi-accurate" sleep for n seconds. It's the best you can do.
I also tried nanosleep with similar increases every time its called:
I was hoping this would sleep somewhere near 1 nanosecond and the first time it is only a few hundred nanoseconds but then the second time its up to hundreds of microseconds...Code:struct timespec in, out; in.tv_sec = 0; in.tv_nsec = 1; nanosleep(&in, &out);
According to this theory, then after a sufficient number of calls it will be sleeping for hours when you ask for 1 nanosecond. I simply don't believe that. How many measurements did you take? I think you're seeing a random trend that means nothing.
Take ten thousand measurements and see if the trend continues. If it does, something is really whacked with your system.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
I don't think you'll ever get it to sleep for 1 nano-second (unless you've got some kind of liquid-nitrogen cooled supercomputer). Most CPU's run at <= 3GHz. Most CPU instructions would take longer than 3 clock ticks, and you'd need a lot of instructions to do a context switch, execute sleep and then context switch again.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
I wrote a piece of code that pauses execution for a given amount of seconds. It's at my home computer, I will post it later.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
This always works for me.Code:#include <time.h> wait(float x) { time_t start; time_t current; time(&start); do time(¤t); while(difftime(current,start) < x); }
It may surprise you how sleep(N) works. It doesn't try to sleep for N amount of time, instead it sleeps for AT LEAST N amount of time, and then returns.
Which means that, with a busy multi-tasking computer, you could have a good deal more sleep time, than you requested, AND in any case, you will ALWAYS lose a tiny amount of time. Even on a computer running no other (non OS) applications.
If you put your sleep() calls, into a loop, you should see some increase in lost time, simply because you have added to the work load of the OS. If the load lightens, the sleep times will get trimmed back down.
That is a busy loop, not a sleep. CPU usage will be maxed out by executing that code. Using such code for a delay of a second is downright cruel!
That technique is only meant for spin-locks where the duration is expected to be typically perhaps less than tenths of a millisecond.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"