Re: How to do atomic read?
- From: Anton Bassov <AntonBassov@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 20 Jun 2007 19:45:24 -0700
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:Yeah, I'd be fascinated by that explanation. But atomic reads are silly.
<0db...@xxxxxxxxx> wrote in message
But, in case I am not seeing the obvious, what's wrong with:First, it's NOT atomic. There is a window for change. This IS atomic:
LONG asignee = InterlockedExchange(&var, var);
LONG asignee = InterlockedCompareExchange(&var, 0, 0);
Thanks for the correction. Can you explain why?
What is the point?
- Follow-Ups:
- Re: How to do atomic read?
- From: Andrew Sha
- Re: How to do atomic read?
- References:
- How to do atomic read?
- From: vasanth
- Re: How to do atomic read?
- From: 0dbell
- Re: How to do atomic read?
- From: Alexander Grigoriev
- Re: How to do atomic read?
- From: 0dbell
- Re: How to do atomic read?
- From: Mark Roddy
- Re: How to do atomic read?
- From: Andrew Sha
- How to do atomic read?
- Prev by Date: Re: How to do atomic read?
- Next by Date: Re: How to do atomic read?
- Previous by thread: Re: How to do atomic read?
- Next by thread: Re: How to do atomic read?
- Index(es):
Relevant Pages
|
Loading