H
Hapa
Does only reading (never writing) of a variable need thread synchronisation?
Thanks for help?
PS.
Anybody knows a Visual C++ news group?
Thanks for help?
PS.
Anybody knows a Visual C++ news group?
That depends on whether you depend on the value read being up to date.Hapa said:Does only reading (never writing) of a variable need thread synchronisation?
Thanks for help?
Does only reading (never writing) of a variable need thread
synchronisation?
It depends. When the variable access can be interrupted a thread switch canHapa said:Does only reading (never writing) of a variable need thread
synchronisation?
Thanks for help?
PS.
Anybody knows a Visual C++ news group?
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.
Gerhard Fiedler said:Can you explain this more? Maybe an example? (Considering that no writes
to
the variable in question occur during the observation period.)
On 2008-06-04 15:59:30, James Kanze wrote:As soon as any threat writes, [...]
thread === threat ?
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 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.
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.
My common understanding of variables are use to store data, so
they will be written sometimes.
We would use a constant instead if they are not written.
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.
Hapa, can you clearify?
I disagree.
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.
This variable is using something like global memory. This
memory is written by a third party app, which takes care that,
we have no collision between two apps. But now I have two or
more threads whitin my app, which will only read this global
memory.
And it said thread synchronization.The question said "only reading, never writing".
Yes, we are interpreting something. Probably we do it in a wrong way.That's not the way I interpreted his question, but I agree
that it could have been clearer.
No, never wrote this, I never cited Posix. I disagree that the single memoryYou disagree with Posix?
You're right. But that's not "something you really can't know". Well, youFor a given compiler, on a given machine, you can sometimes
know.
If a read operation is in a single memory read operation, what do you wantBut it's irrelevant. Even if the variable is in a single
machine word, you need external synchronization.
Sorry, dont know this membar function.I'll occasionally drop down to assembler, and insert a
membar function myself...
Yes, the lock prefix can avoid interrupt (and thread switch) for an(On IA-32 architecture, I think that the lock prefix---implied
on the xchg instruction, will automatically set up some sort of fence.
Threads do
introduce additional complexity, especially with regards to
ensuring correctness, and shouldn't be used unless the cost of
avoiding them outweighs this cost.
news:464682ca-4575-4bf2-90b8-a792c2312b5e@k13g2000hse.googlegroups.com...
No, never wrote this, I never cited Posix.
I disagree that the single memory access is irrelevant. The
fact if you have single memory access it determines the
robustness of the read operation.
With a single memory access it is just impossible that the
problem I described.
You're right. But that's not "something you really can't
know". Well, you said you can know.
If a read operation is in a single memory read operation, what
do you want to synchronize?
Memory.
Sorry, dont know this membar function.
Yes, the lock prefix can avoid interrupt (and thread switch)
for an instruction.
No, the xchg instruction is only used for read and modify.
Aside from the standards issues already raised, consider a singleNo, never wrote this, I never cited Posix. I disagree that the single memory
access is irrelevant. The fact if you have single memory access it
determines the robustness of the read operation.
With a single memory access it is just impossible that the problem I
described.
Aside from the standards issues already raised, consider a
single *misaligned* memory access requiring more than one bus
cycle. A context switch may occur during such a read.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.