Subj : Re: usage of time() for timeout handling To : comp.arch.embedded,comp.programming From : yossi_sr Date : Thu Jul 28 2005 01:09 am David wrote: > Hi gurus, > I'm having some modules, that use time() for determine if a given activity > has timed out. As the operating system time will be updated periodically > with the time or the RTC chip, it is possible that the result of two calls > to time() would give a smaller value on the second call. This would cause > the timeout calculation to fail and result in unexpected behaviour. > This situation could specially occour if the user changed the time of the > system! How are you handling this problem? On could use the system tick for > timeout calculations. But somewhere I read that for acuracy reason you > should not do this for longer timeouts. How do you handle this problem? > > Thanks in adbance > David Managing various timers even with no operating system may be accomplish the following way: 1) In main() every 10msec (tick or 10 ticks of 1msec, you are calling constantly the function which increments all timers in the system ( no matter timer is active or no). 2) You divide your activity as state machine program in which from the moment you want to count time you simply clear the appropriate variable. In SM=0 -> var_timer1=0; ( in Count10msec() -> var_timer1++; ) In SM=1 you check whether var_timer1>msec(1000) if the timeout is 1 sec. If required more timing intervals of the same variable, more states may be added.I am using up to 20-30 timers that way,don't have any problem with this.Normally, only some of the timers are active at the given moment. Of course the resolution is 10msec.I mean that maximum error may be 10 msec because there is no correlation between the aplication and the function which constantly increments all variables. HTH .