"Nadav Har'El" <[EMAIL PROTECTED]> writes:
> On Wed, Nov 03, 2004, Oleg Goldshmidt wrote about "Re: Why not using global
> variables considered thread-safe ?":
> > > On a 1-CPU machine, when you have global variables such as ints or
> > > floats, isn't reading and writing them atomic, and needs no locking?
> >
> > Absolutely not!
> >
> > 1) It is not just reading and writing. Consider, say, incrementing a
> > global variable. It involves a number of operations: fetch from
> > memory into a register, add 1, write back into memory. If there is
> > a context switch in between you are in trouble.
> >
> > 2) What if your memory bus has width less than the width of int? Even
> > reads and writes will not be atomic. Now, how wide are your floats?
>
> The latter is a problem on SMPs, but not on single-CPU machines; Context
> switches are guarenteed to happen after complete machine instructions are
> complete and the instruction and data queues are completely drained - and
> not while machine instructions are still in the middle of being processed.
>
> Of course, some hypothetical machines might have types that do not have
> machine instructions to handle them - e.g., a hypothetical machine might
> not have a single machine instruction to write to a 64-bit double, and
> you need two 32-bit instructions to do that. In that case, you are right.
> But I am not aware of any modern CPU that has such an issue.
Nadav,
You may be right - I cannot make a definite statement without checking
further. However, I have a suspicion that while your statement may be
correct for "normal" types of CPUs and maybe even is part of POSIX or
some other standard, it is not universal. *Possible* exceptions may
include wide types [1], process architectures that do not guarantee
word-alignment [2], and other pathologies.
[1] even "primitive" types wider than int, such as long and double;
AFAIK Java manuals specifically warn that there is no atomic
access to longs and doubles unless you declare them volatile.
[2] yes, there are some, your ints may be not word-aligned
--
Oleg Goldshmidt | [EMAIL PROTECTED]
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]