Given the above code, writes that initialize the Foo instance could be delayed until the write of the instance value, thus creating the possibility that the instance returns an object in an uninitialized state. In order to avoid this, the instance value must be made volatile.
该段话出自
https://www.jetbrains.com/help/resharper/2016.1/PossibleMultipleWriteAccessInDoubleCheckLocking.html
鉴于上面的代码中写道,初始化Foo类的实例可以被推迟,直到实例值的写入;从而有可能出现创建一个实例返回的是未初始化状态的对象,为了避免这种情况,该实例值必须定义成volatile。
问题是,翻译了我也不懂,引用页面中给出的例子错在哪里?
@虾。: 声明 private volatile static Foo instance?
这种问题在c#下应该不会出现的吧,c#的内存模型确保了不可能在double check模式下对象未实例化完成就让其他线程可见吧。