I create a class with singleton design pattern as below
private static Object _lockObj = new Object();
public static T CreateObject()
{
Debug.Print("Thread ID : " + Thread.CurrentThread.ManagedThreadId);
// 'Double checked locking' pattern which avoids locking each
// time the method is invoked
if (_cpSng == null)
{
lock (_lockObj)
{
if (_cpSng == null)
_cpSng = new T();
}
}
return _cpSng;
}
as you see in comment i use double check and and object for avoid locking each time the method is invoked.
But when i use this class in multithread i have never face with an error like locking the object.
So,using double check and object is unnecessary ??
Someone can show me how can i get error without using double check and object.
Thanks.
Aucun commentaire:
Enregistrer un commentaire