c# – 线程安全Singleton:为什么内存模型不能保证其他线程会看到新实例?

栏目: ASP.NET · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/47816720/thread-safe-singleton-why-the-memory-model-does-not-guarantee-that-the-new-inst

我已经阅读了Jon的Skeet在线页面,了解如何在C#中创建线程安全的Singleton

http://csharpindepth.com/Articles/General/Singleton.aspx

// Bad code! Do not use!
public sealed class Singleton
{
    private static Singleton instance=null;

    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

在此代码下面的段落中,它说:

As hinted at before, the above is not thread-safe. Two different  threads could both have evaluated the test if (instance==null) and  found it to be true, then both create instances, which violates the  singleton pattern. Note that in fact the instance may already have  been created before the expression is evaluated, but the memory model  doesn’t guarantee that the new value of instance will be seen by other  threads unless suitable memory barriers have been passed.

你能否解释为什么内存模型不能保证其他线程可以看到实例的新值?

静态变量位于堆上,但为什么不立即与其他线程共享?我们是否需要等待上下文切换,以便其他线程知道实例不再为空?

Can you please explain why doesn’t the memory model does not guarantee that the new value of instance will be seen by other threads?

内存模型很复杂,目前还没有非常清楚地记录,但从根本上来说,很少有情况可以安全地依赖一个线程所写的值,而另一个线程在没有某些锁定或其他线程的情况下被“看到”沟通继续.

例如,考虑一下:

// Bad code, do not use
public class BigLoop
{
    private static bool keepRunning = true;

    public void TightLoop()
    {
        while (keepRunning)
        {
        }
    }

    public void Stop()
    {
        keepRunning = false;
    }
}

如果您创建了两个线程,其中一个调用TightLoop而另一个调用Stop,则无法保证循环方法将终止.

现代CPU中有许多级别的缓存,并且要求每次读取都返回到主内存将消除大量优化.所以我们有内存模型,可以保证哪些变化在什么情况下肯定是可见的.除了这些保证之外,允许JIT编译器假设实际上只有一个线程 – 因此它可以将字段的值缓存在寄存器中,并且永远不会再次访问主存储器,例如.

当前记录的内存模型严重不足,并且表明一些明显奇怪的优化应该是有效的.我不会在那条路上走太远,但是值得阅读Joe Duffy关于 CLR 2.0 memory model 的博客文章.(这比记录的ECMA内存模型更强,但博客文章不是这样一个关键文档的理想位置,我认为还需要更清晰.)

The static variable is located on the heap, but why it is not shared with other threads?

它与其他线程共享 – 但该值不一定立即可见.

翻译自:https://stackoverflow.com/questions/47816720/thread-safe-singleton-why-the-memory-model-does-not-guarantee-that-the-new-inst


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

一目了然

一目了然

Robert Hoekman, Jr. / 段江玲 / 机械工业出版社华章公司 / 2012-3-19 / 59.00元

可用性或易用性是软件或Web设计师的重要设计目标之一。本书深入阐述了如何设计出简单易用的基于Web的软件,以帮助读者理解、掌握显性设计的精髓。作者从软件开发初期谈起,一直到软件设计后期,分析诸多案例并论证了自己的见解或设计原则。本书在第1版的基础上进行了重大改进,尤其是在设计思想上,作者在本书中谈到“以用户为中心的设计”、“以任务为中心的设计”以及“以情景为中心的设计”的理念。这种设计理念也将更直......一起来看看 《一目了然》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试