[Mono-dev] Interlocked

Robert Jordan robertj at gmx.net
Mon May 28 07:18:15 EDT 2007


Hi,

Vladimir Giszpenc wrote:
> Hi,
> 
> This is probably a question for Microsoft, but since I am using Mono I
> will ask you fine folks.  System.Threading.Interlocked.Read()
> addresses 64 bit atomicity.  If I use the shortcut "int", is it
> System.Int32 or System.Int64 depending on the architecture the code is
> compiled on?  If that is the case, shouldn't I be able to use the

In C#, "[u]int" is always System.[U]Int32, regardless of the
architecture.

> Read() with ints as well as long?  Or is reading any kind of int
> always an atomic operation?  I have never even been near a 64 bit
> machine, but I feel I should write code that is portable.

Reading/writing to a System.[U]Int32 field is always atomic,
while System.[U]Int64 ([u]long) is only guaranteed to be atomic
on >= 64-bit architectures.

That said, when you're using ints only, you don't need to care
about the atomicity of a *single* read or write operation.
In all other cases you'll need to use the Interlocked members
or synchronization locks.

Robert




More information about the Mono-devel-list mailing list