public class MySingleton { private static object myLock = new object(); private static volatile MySingleton mySingleton = null; // 'volatile' is unnecessary in .NET 2.0 and later private MySingleton() { } public static MySingleton GetInstance() { if (mySingleton == null) { // 1st check lock (myLock) { if (mySingleton == null) { // 2nd (double) check mySingleton = new MySingleton(); } } } return mySingleton; } }
Thursday, September 25, 2014
Double check singleton design pattern
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment