A
anonieko
PICK ONE
1. Singleton (Thread-safe using double-checking )
--------------------------------------------------
class Singleton
{
private static Singleton instance;
protected Singleton(){}
public static Singleton Instance
{
get
{
if( instance == null )
{
Mutex mutex = new Mutex();
mutex.WaitOne();
if( instance == null )
instance = new Singleton();
mutex.Close();
}
return instance;
}
}
}
// Support multithreaded applications through
// "Double checked locking" pattern which avoids
// locking every time the method is invoked
// Only one thread can obtain a mutex
2: Singleton (Thread-safe guaranteed by CLR )
----------------------------------------------
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
static Singleton() {}
Singleton(){}
public static Singleton Instance
{
get
{
return instance;
}
}
}
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
1. Singleton (Thread-safe using double-checking )
--------------------------------------------------
class Singleton
{
private static Singleton instance;
protected Singleton(){}
public static Singleton Instance
{
get
{
if( instance == null )
{
Mutex mutex = new Mutex();
mutex.WaitOne();
if( instance == null )
instance = new Singleton();
mutex.Close();
}
return instance;
}
}
}
// Support multithreaded applications through
// "Double checked locking" pattern which avoids
// locking every time the method is invoked
// Only one thread can obtain a mutex
2: Singleton (Thread-safe guaranteed by CLR )
----------------------------------------------
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
static Singleton() {}
Singleton(){}
public static Singleton Instance
{
get
{
return instance;
}
}
}
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit