[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: timing ?



Ken Dunn wrote:
> 
> Can anyone see anything wrong with doing this for a simple
> delay between up dates, outside of non-portability ?
> 
> /* for a least 1/20 pause */
> #define USEC_DELAY 50000
> 
> void tick(void)
> {
> struct timeval nt;
> static int64_t old;
> static int64_t new;
> long long t_delta;
> for(;;) {
>         gettimeofday(&nt, NULL);
>         new=(int64_t) nt.tv_usec + (int64_t)\
>                 (1000000 * (int64_t) nt.tv_sec);
>         t_delta = new - old;
                          ^^^^
                  'old' will be uninitialised the first
                  time around the loop - that may or may
                  not matter to you.

>         if(t_delta > (int64_t) USEC_DELAY)
>                 { old = new;
>                   return;
>                 }
>            
>         /* has system time gone backwards ?*/
>         if(t_delta < 0)
>                 { old = new;
>                   continue;
>                 }
>         usleep(1); /* works very nicely on my machine. Where HZ = 1024 */
>         }
> }

-- 
Steve Baker                  http://web2.airmail.net/sjbaker1
sjbaker1@airmail.net (home)  http://www.woodsoup.org/~sbaker
sjbaker@hti.com      (work)