Is assignment of an object reference an atomic operation?

  • Thread starter Thread starter Marcel Ruff
  • Start date Start date
M

Marcel Ruff

Hi,

i have a thread assigning an object:

this.myObj = someObj;

and another thread accessing it, e.g.:

logger.Info(this.myObj.ToString());

Do i need any mutex to protect this or is
the assignment atomic?
Could it be that the first two bytes of the
object reference are written and the second thread
accesses a corrupt reference?

Thanks
Marcel
 
It's unlikely to be atomic - basically the only thing you can assume to be
atomic are interlocked calls, and that's becasue they're explicitly stated
to be atomic. how many operations your example takes is completely unknown.
An operator overload could turn it into a deep copy, which could be easily
thousands of operations.

When doing multi-threading, never assume atomicity. Protect everything.
 
Back
Top