This question may seem a little strange, but is related to possible visibility problems. The question is based on an example in the Java programming language (> jdk5), consider:
public class InmutableValue { private int value; public InmutableValue(int value) {this.value = value;} public int getValue() {return value;} }
Despite the opposite belief, the class above is not thread safe. In a multi-threaded environment, a “value” is not guaranteed to be visible to other threads. In order to make this thread safe, we need to enforce the “happened earlier” rule. This can be achieved by marking the "final" field.
In this case, I wondered if the same is true for the .NET runtime. So take for example:
public class InmutableValue { private int value; public InmutableValue(int value) {this.value = value;} public int Value { get{return value;}} }
As far as I know, marking the value field as "readonly" does not give the same guarantees as "final" for java (but I could be terribly wrong, I hope). So, do I need to mark the fields as "mutable" (or use memory barriers, etc.) to ensure visibility for other threads? Or do other visibility rules apply?
source share