I have field public Action Update; which can be called by multiple threads. Also this field can be changed to null etc. Should I make a property and use a lock statement inside it? Or when I setting Update = new Action (...) it an atomic operation and it doesn’t need a lock?
I have field public Action Update; which can be called by multiple threads. Also
Share
Setting a reference in C# is an atomic operation, so there is no need for a lock. However you should make the
Updatereference volatile to make sure that it is properly refreshed across all threads when changed:Regardless of threading, it is always best practice to not expose members directly but rather use properties. Then you need to make the backing storage for the property volatile, which rules out auto properties: