Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - problems timing using usleep: (2 Items)
   
problems timing using usleep  
I am trying to time a loop.  I want to sleep some so that I am not just spinning and waiting.  The timeout can be 
lengthy, 40 to 50 seconds.

I can't use clock() and usleep() as the latter affects the former (which makes sense I guess, but not what I expected).

I have tried to use clock_gettime() and to use nanosecond math.  That requires large ints and uint64, et. Al. don't seem
 to accept anything larger than a 32 bit value.

I could do a more complex comparison using timespecs without the nanosecond math, but it seems like I'm fighting one 
problem after another.

Am I on the right track at all?  What is the best way to timeout a loop and still include some type of sleep so that I'm
 not constantly spinning?

Thanx
Tim
Re: problems timing using usleep  
I have been successful getting the clock_gettime method using nanoseconds to work (solved the problem with large 
interger math) but I would still be interested in hearing any thoughts if I'm on the right track.  Is there another 
solution which might be a better approach.

Here is a example

while (... )
{
// read some value from a device

usleep(...)

if ( elapsed > timeout )
break;
}

Thanx
Tim