什么是多线程应用程序使用的是辛格尔顿的危险的是、多线程、格尔、应用程序

2023-09-03 12:32:44 作者:默守ゝ納紛愛

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

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.

 
精彩推荐
图片推荐