Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic Assignment of Reference Sized Structs

Tags:

c#

.net

atomic

Quick question... Suppose I have a C# struct that contains a single reference member. Are assignments of this struct atomic in .NET? For Example:

struct ReadOnlyList<T>
{
    private List<T> list;
    // other random functionality
};

I know that plain old reference assignments are atomic, just curious if wrapping the whole thing up in a struct makes any difference...

Thanks

like image 935
LorenVS Avatar asked Jun 22 '26 20:06

LorenVS


1 Answers

12.5 Atomicity of variable references

Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list shall also be atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, need not be atomic. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.

So atomicity of assignment of a structure with the only reference-type member is implementation defined, and you should not rely on it.

like image 115
adontz Avatar answered Jun 24 '26 11:06

adontz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!