In my application (written in C#) I have an instance of a class with a member variable that points to an instance of another class. This second instance is read-only, so the state of that instance will never change once it is created. But on some occasions I want to replace this instance with a new and updated instance. Is replacing this reference in the first instance safe in a multithreaded environment? Or can this cause a problem?
Share
Yes, it is “safe” in the sense that the reference read/write is atomic. Your client code will always get a valid instance.
Obviously you cannot guarantee that the client code will get the new instance immediately.