The C # specification speaks of this, and the same truths basically apply to CIL; in principle, all integer types up to 4 bytes are guaranteed to be atomic, and any read / write links are guaranteed to be atomic.
However, no guarantees have been made outside this; double not guaranteed to be atomic, for example - here Interlocked may be useful.
For a quote from ECMA 335 (sections I-VI of the sections of the common language infrastructure)
12.6.6 Atomic reads and writes
The corresponding CLI must ensure that read and write access to aligned memory cells is no larger than the size of the native word (type native int size) is atomic (see ยง12.6.2) when all write accesses to the location are the same size. An atomic record must not change bits other than those written. If not explicit layout control (see Section II (Layout control instance)) is used to change the default behavior, data elements larger than the actual word size (native int size) should be properly aligned. References to objects should be as if they were stored in the size of the native word.
[Note: there is no guarantee of atomic updating (read-modify-write) of the memory, except for methods provided for this purpose as part of the class library (see Section IV) Atomic recording is a small data elementโ (the element is no larger than the size of the native word) it is required to do atomic read / change / write to hardware that does not support direct writing to small data elements. end note]
[Note: there is no guaranteed atom access to 8-byte data when the int int size is 32 bits, although some implementations can perform atomic operations when the data is aligned on an 8-byte boundary. end note]
Since native int must be at least 32 bits, this is the biggest guarantee he can offer.
Marc gravell
source share