What operations are atomic in C#? What operations are atomic in C#? multithreading multithreading

What operations are atomic in C#?


For something more complete/detailed:

Reads and writes to 32-bit value types are atomic: This includes the following intrinsic value (struct) types: bool, char, byte, sbyte, short, ushort, int, uint, float. The following types (amongst others) are not guaranteed to be atomic: decimal, double, long, ulong.

e.g.

int x;x = 10; // atomicdecimal d;d = 10m; // not atomic

Reference assignment is also an atomic operation:

private String _text;public void Method(String text){  _text = text; // atomic}


Yes. Read the CLI specification: http://www.ecma-international.org/publications/standards/Ecma-335.htm. For instance:

I.12.6.6 Atomic reads and writes

A conforming CLI shall guarantee that read and write access toproperly aligned memory locations no larger than the native word size(the size of type native int) is atomic (see §I.12.6.2) when all thewrite accesses to a location are the same size. Atomic writes shallalter no bits other than those written. Unless explicit layoutcontrol (see Partition II (Controlling Instance Layout)) is used toalter the default behavior, data elements no larger than the naturalword size (the size of a native int) shall be properly aligned.Object references shall be treated as though they are stored in thenative word size.

[Note: There is no guarantee about atomic update(read-modify-write) of memory, except for methods provided for thatpurpose as part of the class library (see Partition IV). An atomicwrite of a “small data item” (an item no larger than the native wordsize) is required to do an atomic read/modify/write on hardware thatdoes not support direct writes to small data items. end note]

[Note:There is no guaranteed atomic access to 8-byte data when the size of anative int is 32 bits even though some implementations might performatomic operations when the data is aligned on an 8-byte boundary. endnote]

Regarding the 64-bit long question, Eric Lippert answers it here: http://blogs.msdn.com/b/ericlippert/archive/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two.aspx

The CLI specification actually makes stronger guarantees. The CLIguarantees that reads and writes of variables of value types that arethe size (or smaller) of the processor's natural pointer size areatomic; if you are running C# code on a 64 bit operating system in a64 bit version of the CLR then reads and writes of 64 bit doubles andlong integers are also guaranteed to be atomic. The C# language doesnot guarantee that, but the runtime spec does. (If you are running C#code in some environment that is not implemented by someimplementation of the CLI then of course you cannot rely upon thatguarantee; contact the vendor who sold you the runtime if you want toknow what guarantees they provide.)

Another subtle point about atomic access is that the underlyingprocessor only guarantees atomicity when the variable being read orwritten is associated with storage that is aligned to the rightlocation in memory. Ultimately the variable will be implemented as apointer to memory somewhere. On a 32 bit operating system, thatpointer has to be evenly divisible by 4 in order for the read or writeto be guaranteed to be atomic, and on a 64 bit operating system it hasto be evenly divisible by 8.


From the CLI specifications you can get here:

"A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size (the size of type native int) is atomic…”

Section 12.5 from the C# specification here:

“Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types.” Also: “…there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.”

Make the increment operation atomic with this.