问题
I want to figure out about singleton pattern designs. I want to create seperated instances for per thread from my singleton class. So I provided two designs below.
It is Working
class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
Console.ReadLine();
}
}
public sealed class SingletonClass
{
[ThreadStatic]
private static SingletonClass _instance;
public static SingletonClass Instance
{
get
{
if (_instance == null)
{
_instance = new SingletonClass();
}
return _instance;
}
}
private SingletonClass()
{
}
}
It is not working (Throwing NullReferenceException and instance is not being created.)
class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
Console.ReadLine();
}
}
public sealed class SingletonClass
{
[ThreadStatic]
private static SingletonClass _instance = new SingletonClass();
public static SingletonClass Instance
{
get
{
return _instance;
}
}
private SingletonClass()
{
}
}
I am really wondering why an instance is not created for second design. Can anybody explain that please ?
回答1:
The answer to your question is mostly related to how class fields are initialized.
In the second example, the _instance
field is initialized at declaration. Every time a static field is initialized at declaration, a static constructor will be created (if you don't have it declared already). At compile time, the initialization will be moved into the static constructor. This means that you will end up having something like this (didn't copy the IL code as it would be harder to understand):
public sealed class SingletonClass
{
[ThreadStatic]
private static SingletonClass _instance;
public static SingletonClass Instance
{
get
{
return _instance;
}
}
static SingletonClass()
{
_instance = new SingletonClass();
}
}
The CLR ensures that the static constructor is called only once, regardless of how many threads you have. Looking at the above code, it means that for the two Tasks that you created, the _instance
field will be initialized only once (since there will be only one call to the static constructor).
回答2:
Instead of using [ThreadStatic]
then you could use ThreadLocal<T>
which will essentially achieve what you're trying with [ThreadStatic]
.
public sealed class SingletonClass
{
private static ThreadLocal<SingletonClass> _instance;
static SingletonClass()
{
_instance = new ThreadLocal<SingletonClass>(() => new SingletonClass());
}
public static SingletonClass Instance
{
get
{
return _instance.Value;
}
}
private SingletonClass()
{
}
}
See: https://msdn.microsoft.com/en-us/library/dd642243(v=vs.110).aspx for more information.
Edit: To answer your question.
In C# when doing:
private static SingletonClass _instance = new SingletonClass();
Regardless if it's marked with [ThreadStatic]
or not then it will only create a single static constructor which sets the instance of SingletonClass
.
C# does not have the ability to create static constructors per threads.
That's what you can use ThreadLocal<T>
for. If we take your code as an example then the default constructor for SingletonClass
essentially becomes the "thread-static"
constructor.
来源:https://stackoverflow.com/questions/47238817/c-sharp-singleton-pattern-designs-for-threadstatic