Re: Only reading of variable vs. thread synchronisation
On Jun 6, 10:29 am, "Helge Kruse" <Helge.Kruse-nos...@gmx.net> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
news:a99b5960-3384-46fc-b6a9-7fde45dc1c1b@l42g2000hsc.googlegroups.com...
Does only reading (never writing) of a variable need
thread synchronisation?
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.
Probably we will have to ask the OP what the question concerned. I
understand that
- there are threads
- one thread is only reading
- it's in question if the reading threads need any synchronization.
The question said "only reading, never writing".
My common understanding of variables are use to store data, so
they will be written sometimes.
Before threading is started, for example. I have a lot of
variables which are only read, never written, once threading has
started.
We would use a constant instead if they are not written.
Unless they need some sort of initialization, which can't be
done until main has been entered.
In practice, of course, a lot of the time, the variables will be
const.
Since threads are in question I assumed that a write access
can occur in another thread. I read the (never writing)
related to the the current thread.
That's not the way I interpreted his question, but I agree that
it could have been clearer.
Hapa, can you clearify?
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.
I disagree.
You disagree with Posix?
After you have read a variable you have a copy of it (at least
to a CPU register). You have to define if a change after the
read is acceptible or not. Of not you need a thread
synchronization. But even if it is acceptible you can get in
trouble as shown.
The compiler defines how data is stored. The statement, if a
variable is in a single word is compiler and CPU dependend.
But you can know.
For a given compiler, on a given machine, you can sometimes
know. But it's irrelevant. Even if the variable is in a single
machine word, you need external synchronization. All Posix
offers here are the pthread_... functions; under Solaris, on a
Sparc, I'll occasionally drop down to assembler, and insert a
membar function myself, but only if it's really, really
necessary. And the compilers I use will never, under any
conditions, generate any membar instruction. (On IA-32
architecture, I think that the lock prefix---implied on the xchg
instruction, will automatically set up some sort of fence. But
again, it's never generated by the compiler, so you either have
to use inline assembler, or one of the system primitives.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=EF=BF=BDe objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=EF=BF=BDmard, 78210 St.-Cyr-l'=EF=BF=BDcole, France, +33 (0)1 30 2=
3 00 34