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 Har'El                        |   Wednesday, Nov 3 2004, 19 Heshvan 5765
[EMAIL PROTECTED]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |I couldn't afford a cool signature, so I
http://nadav.harel.org.il           |just got this one.

=================================================================
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]

Reply via email to