Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are reads/writes of 64-bit values atomic on a 64-bit cpu? [closed]

On 32-bit machines, reading and writing of 64-bit values in .net used to be non-atomic, i.e. when reading, I could read a value that had never been written to memory, consisting of one older 32-bit half, and one newer half.

  1. But are reads/writes atomic on 64-bit cpus?
  2. If so, does the (Windows) OS have to be 64-bit as well?
  3. And if so, would even the app have to be a 64-bit .net build?
like image 254
Evgeniy Berezovsky Avatar asked Dec 03 '25 00:12

Evgeniy Berezovsky


1 Answers

From this answer, if you're on a 64 bit system and using a 64-bit CLR, then they are atomic. The answer references this blog post from Eric Lippert: https://blogs.msdn.microsoft.com/ericlippert/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two/

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

like image 169
kristianp Avatar answered Dec 05 '25 13:12

kristianp



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!