Subj : Re: usage of time() for timeout handling To : comp.arch.embedded,comp.programming From : Dan Henry Date : Wed Jul 27 2005 07:31 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? First off, most of us use something that ticks faster than time(), let's say clock(). Then we make sure to use unsigned arithmetic and always subtract clock values such that an "earlier" clock value is subtracted from a "later" clock value. Something like: if ((now - then) >= TIMEOUT_THRESHOLD) { signal_timeout(); } -- Dan Henry .