views:

106

answers:

2

Let's say I have some variable x (of type myClass), which is initially null and some assignment

x = myObject

occuring exactly once in some background thread.

Is it guaranteed that x always contains either null or myObject when accessed from the main thread? Or is it possible that x contains some invalid data shortly?

+3  A: 

Provided the size of the type reference is the same size as the native word size of the CPU, then the assignment will be atomic (and safe in your described scenario).

In fact, (don't have the reference to hand) I'm fairly certain that a type ref will always be the same size as the native CPU word size.

Mitch Wheat
Here's the reference: Partition I, Section 12.6.6 of the CLI spec states: `A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size is atomic when all the write accesses to a location are the same size.`. Even without quoting the spec you were the first to provide a correct answer.
Darin Dimitrov
@Darin Dimitrov: not sure why you deleted your answer, it was correct after all.
Mitch Wheat
I deleted my answer because it doesn't provide any additional useful information than your answer.
Darin Dimitrov
+1  A: 

From this (although it deals with somewhat different topic) I conclude that assignments are atomc.

Anton Gogolev