views:

116

answers:

5

A quote from MSDN

Assigning an instance of this type is not thread safe on all hardware platforms because the binary representation of that instance might be too large to assign in a single atomic operation.

Does it mean is that Thead-safe on 64bit processors like Itianium or x86-64 as well?

For instance:

long data = GetData();
// some parallel task on data

Could be a problem?

A: 

It's a problem if your data variable is accessed directly by code from different threads.

They don't make guarantees about any specific system, so to be safe I'd assume it's not threadsafe on x64 systems as well as x86, though I suspect what they really had in mind was places like the compact framework running on smart phones.

Joel Coehoorn
but int32 does not have this note hance it must be architecture related (x64 x86)
lukas
+2  A: 

Possibly, but why would you write a program that will be thread safe on some Intel workalike platforms, but not others? Note that the Decimal and Double types also have this disclaimer about thread safety.

Microsoft recommends the use of locks in these cases. There are links to some good information about concurrency, memory mapping and low-level locks here:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f03ea3c9-4c2b-4a79-8f8c-4a6b7476b20d

Robert Harvey
A: 

I would err on the safe side and assume not.

Note this previous question. Essentially, for atomic assignment to Int64s you should look at using the Interlocked class.

Check out this link also for some more detailed analysis. In particular, check out the section labeled: "Atomicity and Interlocked".

Rob Cooke
A: 

The crux of the matter here is that Int64 is not guarenteed to be atomic, but that does not preclude it from being so in some cases. In other words, Int64 would be atomic on 64-bit systems. Of course, that does not mean it is necessarily thread-safe. There are other issues regarding thread-safety aside from atomicity. To guard against the staleness issue, for example, you would have to use the appropriate memory barrier directives (volatile, Thread.VolatileWrite, etc.)

Brian Gideon
A: 

Memory load/store operations are considered to be atomic if they are performed on memory chunks that are placed on aligned memory address and are not bigger than the native machine-sized pointer.
Meaning, at 64bit load/store operation on an aligned memory address will be atomic on a 64bit platform, but it won't be atomic on a 32bit platform.

Modern processors offers a special set of instructions (in .Net, most of them are exposed via the Interlocked class). that allow to achieve atomicity on load/store operations that are larger than the machine's native pointer size (64bit operations on 32bit processors, and 128bit operations on 64bit processors. The latter isn't exposed by the Interlocked class, but is available in native code).

For more details, check Joe Duffy's post: Thread-safety, torn reads, and the like.

Liran