Re: Only reading of variable vs. thread synchronisation
On Jun 5, 4:43 pm, "Helge Kruse" <Helge.Kruse-nos...@gmx.net> wrote:
"Gerhard Fiedler" <geli...@gmail.com> wrote in message
news:f894du98aqf4.dlg@gelists.gmail.com...
On 2008-06-05 05:59:58, Helge Kruse wrote:
Does only reading (never writing) of a variable need
thread synchronisation?
It depends. When the variable access can be interrupted a
thread switch can occure. If you need more than one
instruction to read _the_actual_data_ you can read one part
of the variable before and one after the read. This can
give strange results.
Can you explain this more? Maybe an example? (Considering
that no writes to the variable in question occur during the
observation period.)
The thread switch can occur at any point of time, after each
CPU instruction.
We all know that, but it's irrelevant here. The question
concerned what happens if no one writes to the variable. If no
thread modifies the variable, Posix (and Windows as well, I
think) guarantees that non-synchronized accesses work. You
don't need to know or understand more than that---the compiler
will do whatever is necessary for them to work. If any thread
modifies the variable, both Posix and Windows require
synchronization. Even if the variable is in a single machine
word (something you really can't know). Whether the variable is
in a single machine word or not is totally irrelevant here.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34