C# object type - atomicity of assignment -


quick question. below assignment atomic:

object [] table = new object[10] ... table[3] = 10; //is atomic ? ... 

i assuming op means 'thread safe' when talking atomicity.

a write operation not atomic if possible thread can read partially written value.

if object [] table local method, each thread own table , hence operation on table atomic.

going forward assuming table shared across threads.

op has defined table array of object. hence table[3] = 10 involves boxing.

even though table[3] = 10 represents chain of instructions, operation atomic because writing 'address' of boxed instance (current clr implementation represent object reference using memory address) , addresses of natural word size of machine (i.e. 32 bit on 32 bit machines , 64 bit on 64 bit machines). please note though above explanation based on current clr implementation, atomicity of reference writing guaranteed specs. operation of boxing , boxed instance local thread , hence there no way thread can interfere in that. going same reasoning if value exceeding word size (for e.g. decimal) being written, operation have been atomic (due boxing). must noted here above argument holds if value being written has been obtained in thread safe manner.

had no boxing been involved, usual rules of word size writes (or sizes lesser word size, owing memory alignment) being atomic holds.