Is assignment of an object reference an atomic operation?

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top