首页 新闻 会员 周边

帮忙解释一下下面页面中的内容。有关线程安全、双检测的

0
悬赏园豆:20 [已关闭问题] 关闭于 2016-07-11 11:45
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

虾。的主页 虾。 | 菜鸟二级 | 园豆:257
提问于:2016-06-29 17:58
< >
分享
所有回答(2)
0

 

 

鉴于上面的代码中写道,初始化Foo类的实例可以被推迟,直到实例值的写入;从而有可能出现创建一个实例返回的是未初始化状态的对象,为了避免这种情况,该实例值必须定义成volatile。

 

 

JaneEyre | 园豆:454 (菜鸟二级) | 2016-06-29 18:14

问题是,翻译了我也不懂,引用页面中给出的例子错在哪里?

支持(0) 反对(0) 虾。 | 园豆:257 (菜鸟二级) | 2016-06-29 18:26

@虾。: 声明  private volatile static Foo instance?

支持(0) 反对(0) JaneEyre | 园豆:454 (菜鸟二级) | 2016-06-29 22:18
0

这种问题在c#下应该不会出现的吧,c#的内存模型确保了不可能在double check模式下对象未实例化完成就让其他线程可见吧。

Daniel Cai | 园豆:10424 (专家六级) | 2016-06-30 13:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册