什么是多线程应用程序使用的是辛格尔顿的危险 [英] What are the Dangers of using a Singleton in a multithreaded application

查看:113
本文介绍了什么是多线程应用程序使用的是辛格尔顿的危险的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在做记录多线程运服务采用单,想知道什么是一些我可能会遇到的问题。我已经建立了获得实例来处理与同步

I'm looking at using a singleton in a multithreaded Win service for doing logging, and wanted to know what are some of the problems I might encounter. I have already set up the get instance to handle syncing with

    private static volatile Logging _instance;
    private static object _syncRoot = new object();

    private Logging(){}
    public static Logging Instance
    {
        get
        {
            if (_instance==null)
            {
                lock(_syncRoot)
                {
                    if (_instance == null)
                    {
                        _instance = new Logging();
                    }
                }
            }
            return _instance;
        }
    }

还有什么我可能需要担心?

Is there anything else I might need to worry about?

推荐答案

这看起来pretty的对我好。

That looks pretty good to me.

请参阅实现在C# Singleton模式以获得更多信息。

See Implementing the Singleton Pattern in C# for more info.

编辑:也许应该把锁内的回报率,但

Should probably put the return inside the lock, though.

这篇关于什么是多线程应用程序使用的是辛格尔顿的危险的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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