Files
GX.O3.WMS/APP.O3/Shared/Singleton.cs
T

35 lines
880 B
C#
Executable File

using System.Threading;
namespace Opsoft.NetCore.O3App.Shared
{
internal class Singleton<T> where T : class, new()
{
private static T _instance;
private static object _syncRoot = new object();
public static T Instance
{
get
{
T instance = _instance;
if (instance == null)
{
lock (_syncRoot)
{
instance = Volatile.Read<T>(ref _instance);
if (instance == null)
{
instance = new T();
}
Volatile.Write<T>(ref _instance, instance);
return instance;
}
}
return instance;
}
}
}
}