Subj : Re: usage of time() for timeout handling To : comp.arch.embedded,comp.programming From : Lanarcam Date : Wed Jul 27 2005 07:01 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? Whithout more context this is difficult to give an answer. On some systems I increment a 32 bit counter at each interrupt of the real time clock say every 10ms. You can always compute the difference between the values of the counter at each call. You increment the counter in the realtime interrupt ISR. You can read the counter from a user task but you must take into account the possibility that your task will be interrupted by the ISR. If the size of the counter is an int, say 32 bits on a 32 bits processor the incrementation will be atomic and you will always read correct values. otherwise if you are using a 32 bit counter on a 8 bit processor, you should read the counter value twice from the user task and verify that you have the same value, otherwise you read again. Even if the counter rolls over you can compute the difference if you use unsigned integers. This method is independant of the actual time of the system. Even if the user changes the time the counter will not be modified. HTH .