public static SingletonThreadSafe GetInstance() { // this will create two instances running in parallel //if (_instance == null) //{ // if (_instance == null) // { // _instance = new SingletonThreadSafe(); // } //} //return _instance; // double-checked locking for better performance if (_instance == null) { lock (_instanceLock) // lock lower the performance { if (_instance == null) { _instance = new SingletonThreadSafe(); } } } return(_instance); }
public static void FromB() { var singleton2 = SingletonThreadSafe.GetInstance(); singleton2.IncrementCount("B"); singleton2.IncrementCount("B"); singleton2.IncrementCount("B"); }
public static void FromA() { var singleton1 = SingletonThreadSafe.GetInstance(); singleton1.IncrementCount("A"); singleton1.IncrementCount("A"); singleton1.IncrementCount("A"); }