Timer "wraps"?
Scott M
Posts: 43
It looks like the timer class implements a 32 bit timer. This means the timer wraps every 10.5 hours or so. (I'm rounding off to keep the numbers simple, here).
This should raise all sorts of problems in my application. If it's been running for, say, 10.4999 hours, and I then set a timer for 10 minutes, I'm going to guess that things are going to fail, badly. The math will wrap and the very next timeout check I do is likely to return true. Is this correct?
I can think of ways to write the Timer class differently, that would avoid this problem (well, mostly: there is no simple fix that doesn't make at least some assumptions.) Is this a known problem, and has anyone already come up with a fix?
This should raise all sorts of problems in my application. If it's been running for, say, 10.4999 hours, and I then set a timer for 10 minutes, I'm going to guess that things are going to fail, badly. The math will wrap and the very next timeout check I do is likely to return true. Is this correct?
I can think of ways to write the Timer class differently, that would avoid this problem (well, mostly: there is no simple fix that doesn't make at least some assumptions.) Is this a known problem, and has anyone already come up with a fix?
Comments
timeout() method calculates the differences with respect
to the current timer value.
You only have a problem if you want a timeout longer than
the 10.5 hours. Then you must keep the time in a
private variable. For example, if you want a timeout for
24 hours, you should test for a timeout of 1 hour, and
then increment your private hour counter. Then, when
that reaches 24, clear it and execute your code you
want to run once every 24 hours.
The problem with those longer timeout values is the
required 32bit integer math. Normally, with longer
intervals you don't need the 8.68 microsecond resolution
of the 32bit timer. For that purpose I have written a 16bit
timer class that allows selectable time units up to 0.6 seconds
per tick (65536 * 0.6 sec = your 10.5 hour).
Check out my ScaledTimer16 class here:
http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/core/
regards peter