Re: How to do atomic read?



rather than by InterlockedXXX functions. These functions use LOCK prefix
that

not all of them

"Anton Bassov" <AntonBassov@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F4120CD8-0F84-48CC-8AE5-FCFD99D831B0@xxxxxxxxxxxxxxxx
But atomic reads are silly.

Actually, they are not, but in context of this discussion they are -
atomicity of reads and writes should be achieved by proper aligninment,
rather than by InterlockedXXX functions. These functions use LOCK prefix
that
does not apply to MOV instruction, i.e. to reads and writes - if you use
it
with MOV, CPU is going to raise an exception. Therefore, InterlockedXXX
functions should be used not for reads and writes but for increments,
decrements, exchanges and other operations
that can be used with LOCK prefix ( please consult Volume 2 of Intel
Developer's Manual for the list of instructions that can be used with LOCK
prefix )


When it comes to atomicity of reads and writes, it should be achieved
simply
by proper aligninment. One-byte memory access with MOV instruction is
always
atomic, as well as as WORD and DWORD ones that are aligned on respectively
WORD and DWORD boundary.

Anton Bassov


"Mark Roddy" wrote:

0dbell@xxxxxxxxx wrote:
On Jun 20, 9:11 am, "Alexander Grigoriev" <a...@xxxxxxxxxxxxx> wrote:
<0db...@xxxxxxxxx> wrote in message

But, in case I am not seeing the obvious, what's wrong with:
LONG asignee = InterlockedExchange(&var, var);
First, it's NOT atomic. There is a window for change. This IS atomic:

LONG asignee = InterlockedCompareExchange(&var, 0, 0);

Thanks for the correction. Can you explain why?

Yeah, I'd be fascinated by that explanation. But atomic reads are silly.
What is the point?



.



Relevant Pages

  • Re: shared_ptr from dinkumware ... comments??
    ... architecture, but when I worked on it, you needed a lock prefix ... to guarantee atomicity. ... no CAS is used... ... instruction which isn't widely available. ...
    (comp.programming.threads)
  • Re: How to do atomic read?
    ... For example, if InterlockedExchangerelied upon XCHG instruction, as ... it name suggests, it would not have to use LOCK prefix, because LOCK is ... Therefore, InterlockedXXX ... When it comes to atomicity of reads and writes, ...
    (microsoft.public.development.device.drivers)