.NET 中的双重检查锁定 [英] Double-checked locking in .NET

查看:26
本文介绍了.NET 中的双重检查锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章,讨论了为什么双重检查锁定范例在 Java 中被破坏.如果变量声明为 volatile,范式是否适用于 .NET(特别是 C#)?

I came across this article discussing why the double-check locking paradigm is broken in Java. Is the paradigm valid for .NET (in particular, C#), if variables are declared volatile?

推荐答案

在 C# 中实现单例模式 在第三个版本中谈到了这个问题.

Implementing the Singleton Pattern in C# talks about this problem in the third version.

它说:

使实例变量 volatile 可以使其工作,就像显式内存屏障调用一样,尽管在后一种情况下,即使专家也无法确切地同意需要哪些屏障.我倾向于避免出现专家不同意什么是对什么是错的情况!

Making the instance variable volatile can make it work, as would explicit memory barrier calls, although in the latter case even experts can't agree exactly which barriers are required. I tend to try to avoid situations where experts don't agree what's right and what's wrong!

作者似乎暗示双重锁定比其他策略更不可能奏效,因此不应使用.

The author seems to imply that double locking is less likely to work than other strategies and thus should not be used.

这篇关于.NET 中的双重检查锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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