Re: How to do atomic read?



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

not all of them

Actually, it looks like they all do (at least in the user mode under XP
SP2), although,
according to their names, not all of them have to - I fully agree with you
here. For example, if InterlockedExchange() relied upon XCHG instruction, as
it name suggests, it would not have to use LOCK prefix, because LOCK is
automatically assumed for XCHG instruction. However, for this or that reason
it uses CMPXCHG exactly like InterlockedCompareExchange() does, so that it
needs LOCK prefix.

Anton Bassov

"Andrew Sha" wrote:

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: atomic increment and fetch on intel ia32
    ... Is "inc mem" some kind of assembly instruction on x86? ... What do you mean by a lock prefix? ... Anyway, x86 inc can increment a register or a memory location, and can ...
    (comp.programming.threads)
  • Re: Memory ordering and flag/state variables
    ... cache line. ... and is of particular importance to operations that cannot ... InterlockedExchangepre-pends LOCK prefix to CMPXCHG instruction, ...
    (microsoft.public.development.device.drivers)
  • Re: How to do atomic read?
    ... that can be used with LOCK prefix (please consult Volume 2 of Intel ... When it comes to atomicity of reads and writes, ... One-byte memory access with MOV instruction is ... LONG asignee = InterlockedCompareExchange; ...
    (microsoft.public.development.device.drivers)
  • Re: Load ordering by introducing dependencies
    ... This instruction can be used with a LOCK prefix to allow the ...
    (comp.lang.asm.x86)

Loading