Volatile fields in C#
Posted
by
Danny Chen
on Stack Overflow
See other posts from Stack Overflow
or by Danny Chen
Published on 2011-02-25T03:41:04Z
Indexed on
2012/11/12
11:02 UTC
Read the original article
Hit count: 306
From the specification 10.5.3 Volatile fields:
The type of a volatile field must be one of the following:
A reference-type.
The type byte, sbyte, short, ushort, int, uint, char, float, bool, System.IntPtr, or System.UIntPtr.
An enum-type having an enum base type of byte, sbyte, short, ushort, int, or uint.
First I want to confirm my understanding is correct: I guess the above types can be volatile because they are stored as a 4-bytes unit in memory(for reference types because of its address), which guarantees the read/write operation is atomic. A double/long/etc type can't be volatile because they are not atomic reading/writing since they are more than 4 bytes in memory. Is my understanding correct?
And the second, if the first guess is correct, why a user defined struct with only one int
field in it(or something similar, 4 bytes is ok) can't be volatile? Theoretically it's atomic right? Or it's not allowed simply because that all user defined structs(which is possibly more than 4 bytes) are not allowed to volatile by design?
© Stack Overflow or respective owner