Re: Problem with volatile modifier
From: Marcin Kalicinski (kalita_at_poczta.onet.pl)
Date: Fri, 1 Oct 2004 11:02:07 +0200
> int clock, rolls;
> clock = read_clock();
> rolls = read_rolls();
> } while (clock != read_clock());
This approach will create an infinite loop because clock is updated by
hardware very often (roughly at the speed of CPU clock) and will certainly
change between two read_clock() calls in the above loop. So, the condition
in while will always be met.
> > atomically_, and your approach is flawed, as you have two threads of
> > execution trying to update flag. What you have to do is read both in a
> > loop, exiting when two consective reads are the same for both, then
No, I don't have threads. The situation is different because interrupts are
synchronous unlike threads. If interrupt handler is executing, the main
program is stopped until the handler finishes. Handler cannot be interrupted
in the middle (at least not to return control back to main program).
What I think now is that my approach to use this little hardware clock as a
long-term timer is doomed, because from time to time other interrupts with
greater priority happen, and prevent calling of handler() on clock overflow.
Thus, the clock misses some overflows.