From: Jujitsu Lizard on
I'm looking at InterlockedExchange(), InterlockedIncrement(), etc.

Just one question ... am I guaranteed that a read of a 32-bit variable on a
Win32 platform is atomic?

I was kind of looking for an InterlockedGet() function, but none seems to
exist.

I did find this in the Microsoft MSDN documentation:

<BEGIN>
Simple reads and writes to properly-aligned 32-bit variables are atomic
operations. In other words, you will not end up with only one portion of the
variable updated; all bits are updated in an atomic fashion. However, access
is not guaranteed to be synchronized. If two threads are reading and writing
from the same variable, you cannot determine if one thread will perform its
read operation before the other performs its write operation.
<END>

Writes are a bit of a moot point, as one can use InterlockedExchange() to do
this if one is paranoid. But reads cause me to have more questions.

So, I'm assuming I can safely do this:

LONG volatile x;

if (x != 0)
{
...
}

or perhaps if I'm extra paranoid:

LONG volatile x; //Used by another thread.
LONG volatile y; //Not used by another thread, but use volatile to be sure
no optimization nonsense delaying the copy out.

y = x;

if (y != 0)
{
...
}

Thanks for any insight.

The Lizard

From: Boris on
"Jujitsu Lizard" <jujitsu.lizard(a)gmail.com> wrote in message
news:zv2dnW0-IuvYfNjUnZ2dnUVZ_sHinZ2d(a)giganews.com...
> I'm looking at InterlockedExchange(), InterlockedIncrement(), etc.
>
> Just one question ... am I guaranteed that a read of a 32-bit variable on
> a Win32 platform is atomic?
>
> I was kind of looking for an InterlockedGet() function, but none seems to
> exist.
>
> I did find this in the Microsoft MSDN documentation:
>
> <BEGIN>
> Simple reads and writes to properly-aligned 32-bit variables are atomic
> operations. In other words, you will not end up with only one portion of
> the variable updated; all bits are updated in an atomic fashion. However,
> access is not guaranteed to be synchronized. If two threads are reading
> and writing from the same variable, you cannot determine if one thread
> will perform its read operation before the other performs its write
> operation.
> <END>
>
> Writes are a bit of a moot point, as one can use InterlockedExchange() to
> do this if one is paranoid. But reads cause me to have more questions.
>
> So, I'm assuming I can safely do this:
>
> LONG volatile x;
>
> if (x != 0)
> {
> ...
> }
>
> or perhaps if I'm extra paranoid:
>
> LONG volatile x; //Used by another thread.
> LONG volatile y; //Not used by another thread, but use volatile to be
> sure no optimization nonsense delaying the copy out.
>
> y = x;
>
> if (y != 0)
> {
> ...
> }
>
> Thanks for any insight.
>
> The Lizard
As long as the value of variable was commited, it can be just accessed by
simple read.
By value being commited I mean any one of these:
1. Interlocked-Increment/Decrement/Exchange (if the variable is properly
aligned - which is default case);
2. Variable value is just simply written, and the variable is 'volatile',
and it's Visual C++ 2005 or above;
3. Mutex/Critical Section was acquired, the variable value was updated, and
the Mutex/Critical Section released;

Boris