Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic read/write size on 64bit systems for .NET?

Tags:

c#

.net

64-bit

The C# Specification says that reading and writing is atomic for 32bit types and smaller (as well as for references). So if I have an Int32 field in my class I know that multiple threads can read and write to it and they will be atomic in operation and so the value will always be consistent (although caching issues are a potential problem but that is not the point of this question).

Is this the same on 64bit systems as well? If I compile my app for 64bit does that mean an Int64 is still considered to not be atomic? Or can I now consider an Int64 to be atomic in read/write because it is compiled for and running on a 64bit system?

like image 512
Phil Wright Avatar asked Oct 19 '11 22:10

Phil Wright


1 Answers

It should be the same (not atomic) - values must be aligned properly to allow 64 bit values to have atomic read/write, but as far as I know there is no requirement for CLR to always align Int64 values that way.

Check out How to guarantee 64-bit writes are atomic? for some discussion on it.

like image 72
Alexei Levenkov Avatar answered Nov 19 '22 14:11

Alexei Levenkov