C
Chris Fink
I am trying to implement a singleton pattern in my application. I need to
make sure that this class is both thread safe and ensure that only 1 instance
can created at any given time.
As you can see below I create a two instances of the singleton class, s1 and
s2. Both s1 and s2 are indeed the same instance as I expected. However,
when s2 changes the Count to 2, the count for S1 also changes to 2!!! This is
the part I am confused about since I would expect this singleton to be thread
safe....the fact that the s2 instance changed the value in s1 will cause
problems in a multithreaded app. Am I missing something here? How do I
prevent s2 and subsequent instances making changes to my singleton while s1
has a lock on it?
==== my singleton class ====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Singleton
{
private static Singleton instance;
// Note: Constructor is 'protected'
protected Singleton()
{
}
public int Count { get; set; }
public static Singleton Instance()
{
lock(typeof(Singleton))
{
// Use Lazy initialization
if (instance == null)
instance = new Singleton();
lock(instance)
{
return instance;
}
}
}
}
==== test code ====
// Constructor is protected -- cannot use new
Singleton s1 = Singleton.Instance();
s1.Count = 1;
Singleton s2 = Singleton.Instance();
s2.Count = 2;
int i = s1.Count; //2nd instance changed my s1 count? I thought
locking would prevent this?
if (s1 == s2)
{
Console.WriteLine("Objects are the same instance");
}
// Wait for user
Console.Read();
make sure that this class is both thread safe and ensure that only 1 instance
can created at any given time.
As you can see below I create a two instances of the singleton class, s1 and
s2. Both s1 and s2 are indeed the same instance as I expected. However,
when s2 changes the Count to 2, the count for S1 also changes to 2!!! This is
the part I am confused about since I would expect this singleton to be thread
safe....the fact that the s2 instance changed the value in s1 will cause
problems in a multithreaded app. Am I missing something here? How do I
prevent s2 and subsequent instances making changes to my singleton while s1
has a lock on it?
==== my singleton class ====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Singleton
{
private static Singleton instance;
// Note: Constructor is 'protected'
protected Singleton()
{
}
public int Count { get; set; }
public static Singleton Instance()
{
lock(typeof(Singleton))
{
// Use Lazy initialization
if (instance == null)
instance = new Singleton();
lock(instance)
{
return instance;
}
}
}
}
==== test code ====
// Constructor is protected -- cannot use new
Singleton s1 = Singleton.Instance();
s1.Count = 1;
Singleton s2 = Singleton.Instance();
s2.Count = 2;
int i = s1.Count; //2nd instance changed my s1 count? I thought
locking would prevent this?
if (s1 == s2)
{
Console.WriteLine("Objects are the same instance");
}
// Wait for user
Console.Read();