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?
That looks pretty good to me.
See Implementing the Singleton Pattern in C# for more info.
Edit: Should probably put the return inside the lock, though.