是“双重检查锁定被破坏"仅限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. 实例变量被声明为volatile,并且
  2. JVM 正确实现了 JSR-133 规范;即它与 Java 5 及更高版本兼容.

我的来源是 JSR-133(Java 内存模型)常见问题解答 - Jeremy Manson 和 Brian Goetz,2004 年 2 月.Goetz 在其他许多地方都证实了这一点.

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.

但是,正如 Goetz 所说,这是一个已经过时的成语.Java 中的无争用同步现在很快,所以他建议如果您需要进行延迟初始化,您只需将 getInstance() 方法声明为 synchronized.(我想这也适用于其他语言......)

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# - 根据 https://stackoverflow.com/a/1964832/139985,它是取决于平台是否实例变量需要是可变的.但是,Wikipedia 说,如果你这样做使用volatile或显式内存屏障,成语可以安全实现.

  • C# - according to https://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 - 根据 维基百科 成语可以使用显式内存屏障安全地实现.

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

C++ - 根据 维基百科在 Visual C++ 2005 中使用 volatile 可以安全地实现 idiom.但其他消息来源说一般 C++ 语言规范没有为 volatile 确定.然而,双重检查锁定可以在 C++ 2011 语言修订版的上下文中实现 - https://stackoverflow.com/a/6099828/139985.

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 - https://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.)

相关文章