[Mono-dev] Interlocked

Reiterer, Horst horst.reiterer at fabasoft.com
Mon May 28 05:10:18 EDT 2007


> If I use the shortcut "int", is it System.Int32 or System.Int64
> depending on the architecture the code is compiled on?

The keyword 'int' is an alias for System.Int32, independent from the underlying platform. Therefore, you can rely on the fact that it represents a 32-bit (signed) integer.

There are types of variable width in the type system, namely System.IntPtr and System.UIntPtr. However, these are mainly used in interoperability scenarios, in the context of PInvoke.

> If that is the case, shouldn't I be able to use the Read() with ints
> as well as long? Or is reading any kind of int always an atomic
> operation? 

The Common Language Infrastructure (CLI) specification mandates that "read and write access to properly aligned memory locations no larger than native word size (the size of type native int) is atomic". However, "There is no guaranteed atomic access to 8-byte data when the size of a native int is 32 bits even though some implementations may perform atomic operations when the data is aligned on an 8-byte boundary."

So, the runtime guarantees that reads and writes to variables of type 'int' are atomic. There's no such guarantee for System.{Int64,Uint64} because not all hardware platforms can provide it without additional atomic instructions added by the JIT, which is why System.Threading.Interlocked.Read exists (only for System.Int64).

Hope this helps!



Horst Reiterer
Research & Development
Fabasoft R&D GmbH
Honauerstraße 4
4020 Linz
Tel: [43] (732) 60 61 62
Fax: [43] (732) 60 61 62-609
E-Mail: horst.reiterer at fabasoft.com

Handelsgericht Linz, FN 271303a

Fabasoft What's New Briefing 7.0
2007-06-13, 09:30am - 05:00pm
Design Center Linz, Europaplatz 1, 4020 Linz, Austria

More information about the Mono-devel-list mailing list