C # atomicity: int vs. purpose long on x64

I look at http://msdn.microsoft.com/en-us/library/aa691278(VS.71).aspx , which says that reading and writing int is atomic, while reading and writing may not be long. Is this really true for a 64-bit platform? Isn't it IntPtr.Size == 64 bits or long? Am I missing something, or are the language specifications not complete enough?

Other thoughts here: http://philosopherdeveloper.wordpress.com/2011/02/08/beware-assignment-atomic-assignment/

+4
source share
2 answers

long is atomic notation on CPU platforms and with 64-bit words. for example, if you run a 32-bit .NET application on a 64-bit computer, writing to long will not be atomic.

Remember that atomicity is almost meaningless if you cannot make sure that the compiler does not optimize access to this type of variable. for example, you cannot decorate a long volatile field.

Operations on 64-bit fields are guaranteed to be atomic only in an environment with a 64-bit environment

http://www.albahari.com/threading/part4.aspx

if you use C # code on a 64-bit operating system on a 64-bit version of the CLR, then reading and writing 64-bit doubles and long integers is also guaranteed to be atomic

http://blogs.msdn.com/b/ericlippert/archive/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two.aspx

+12
source

This is the documentation for VS2003, which complies with the .NET Framework 1.1, which was only available in a 32-bit style (ignoring niche versions of IA-64 now).

The .NET Framework 2.0 introduced x64, where (while you are working in 64-bit mode), some (but not all) Int64 operations will be atomic.

Of course, if you want to be sure, use the Interlocked class.

0
source

Source: https://habr.com/ru/post/1434960/


All Articles