是"双检锁是破碎"仅Java的事情吗? [英] is "Double-Checked Locking is Broken" a java-only thing?

查看:160
本文介绍了是"双检锁是破碎"仅Java的事情吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.cs.umd.edu/~pugh/页面的Java / memoryModel / DoubleCheckedLocking.html 说,双重检查锁定在Java缺陷。我只是想知道是否也适用于其他语言(C#,VB,C ++等)

the page at http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html says that double-checked locking is flawed in java. I'm just wondering does it also apply to other languages (C#, Vb, C++, etc)

我读过的双重检查锁定模式:分拆与否,的这是打破双重检查锁定?,的如何解决"双检锁是破碎"在Java中声明?的是真实的我不知道的共识是什么。有人说是它打破别人说没有。

I've read Double checked locking pattern: Broken or not?, Is this broken double checked locking?, How to solve the "Double-Checked Locking is Broken" Declaration in Java? to be truthful i don't know what the common consensus is. some say yes its broken others say no.

不管怎样,我的问题是,它也适用于其他语言(C#,VB,C ++等)

Anyway, my question is does it also apply to other languages (C#, Vb, C++, etc)

推荐答案

双重检查锁定在Java安全的,但前提是:

Double checked locking is safe in Java, PROVIDED THAT:


  1. 的实例变量声明为挥发性

  2. 的JVM正确地实现了JSR-133规范。也就是说,它是符合Java 5及更高版本

我的来源是的 JSR-133(Java内存模型)常见问题 - 曼森杰里米和布赖恩戈茨,2004年2月。这是通过戈茨在许多其他地方证实。

My source is the JSR-133 (Java Memory Model) FAQ - Jeremy Manson and Brian Goetz, February 2004. This is confirmed by Goetz in a number of other places.

不过,正如戈茨说,这是一个成语的时代已经过去了。在Java中竞争的同步现在是快,所以他建议,你只需要声明的的getInstance()方法同步如果您需要做延迟初始化。 (和我想象,这适用于其他语言太...)

However, as Goetz says, this is an idiom whose time has passed. Uncontended synchronization in Java is now fast, so he recommends that you just declare the getInstance() method as synchronized if you need to do lazy initialization. (And I imagine that this applies to other languages too ...)

此外,所有的事情都是平等的,这是一个坏主意编写Java 5中运行的代码但在较旧的JVM不可靠的。

Besides, all things being equal, it is a bad idea to write code that works in Java 5 but is unreliable in older JVMs.

确定,那么,关于其他语言?嗯,这取决于的如何的成语的实施,并经常在平台上。

OK, so what about the other languages? Well, it depends on how the idiom is implemented, and often on the platform.

  • C# - according to http://stackoverflow.com/a/1964832/139985, it is platform dependent whether the instance variable needs to be volatile. However, Wikipedia says that if you do use volatile or explicit memory barriers, the idiom can be implemented safely.

VB - 根据<一个HREF =http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Microsoft_.NET_.28Visual_Basic.2C_C.23.29相对=nofollow>维基百科的成语可以安全地实现使用显式内存屏障

VB - according to Wikipedia the idiom can be implemented safely using explicit memory barriers.

C ++ - 根据的维基百科的成语可以安全使用实施挥发性在Visual C ++ 2005但其他消息人士说,一般 C ++语言规范没有提供挥发性是肯定的。然而双重检查锁定的可以的在C ++语言2011修订范围内实施 - 的http://计算器.COM / A /十三万九千九百八十五分之六百零九万九千八百二十八

C++ - according to Wikipedia the idiom can be implemented safely using volatile in Visual C++ 2005. But other sources say that in general the C++ language specification doesn't provide sufficient guarantees for volatile to be sure. However double-checked locking can be implemented in the context of the C++ 2011 language revision - http://stackoverflow.com/a/6099828/139985.

(注:我只是总结一些消息来源我发现这在我看来是近期的...和声音。我不是C ++,C#或VB专家,请阅读链接的网页,让你自己判断。)

(Note: I'm just summarizing some sources I found which seem to me to be recent ... and sound. I'm not C++, C# or VB expert. Please read the linked pages and make your own judgements.)

这篇关于是&QUOT;双检锁是破碎&QUOT;仅Java的事情吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆