.net: Does the reference link to the object (i.e., thread-safe)?

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

 x = myObject 

happens exactly once in some background thread.

Is it guaranteed that x always contains either null or myObject when accessing from the main thread? Or is it possible that x contains some invalid data in the near future?

+7
multithreading
source share
2 answers

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

Actually (I have no link to the hand). I am sure that the ref type will always be the same size as the native word size of the processor.

+6
source share

From this (although it deals with a slightly different topic), I conclude that the destination is atomc.

+1
source share

All Articles