Thread safe Singleton Patterns in c#

Here are two thread safe singleton patterns.  The first is using a Monitor lock and the second is using static initialization.  Both are thread safe and do not use Double-checked locking which does not work.  This is a common question, so I post to keep a URL handy:

/// This is a thread-safe implementation of Singleton using a Monitor.
/// </summary>
public sealed class SingletonLock
{
 private static SingletonLock instance = null;
 private static readonly object syncRoot = new object();

 // Don’t allow instance to be created by user.
 private SingletonLock()
 {
 }

 public static SingletonLock Instance
 {
  // Note: We don’t try to use double-checked locking which does *not work.
  // We explicitly take out a lock before checking if the instance is null.
  get
  {
   lock(syncRoot)
   {
    if ( instance == null )
     instance = new SingletonLock();
    return instance;
   }
  }
 }
}

 /// <summary>
 /// Thread safe Singleton using static instance.
 /// Static constructors are specified to execute only when an instance
 /// of the class is created or a static member is referenced.  For us, this means
 /// instance can only be created once and is created with thread safety.
 /// </summary>
 public sealed class Singleton
 {
  private static Singleton instance = new Singleton();

  // Explicit static constructor to tell C# compiler
  // not to mark type as beforefieldinit.
  static Singleton()
  {
  }

  // Private constructor so users can not create instance.
  private Singleton()
  {
  }

  public static Singleton Instance
  {
   get { return instance; }
  }
 }

The end result is the same as an instance is created in a thread safe way.   I tend to favor the static method as it does not require the lock and is bit easier to remember.  Also see these two articles on some reasons why DCL does not work:
http://www-106.ibm.com/developerworks/java/library/j-dcl.html
http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double-p2.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconthreadingdesignguidelines.asp

[Author Update] I guess the DCL patten can work in .Net as the CLR handles this correctly (If you believe the article).  You still need to use a volatile var which is not free, but should be faster then taking out a lock each time.:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp

— William Stacey [MVP]

Advertisements
This entry was posted in C#. Bookmark the permalink.

31 Responses to Thread safe Singleton Patterns in c#

  1. Unknown says:

    http://www.batteryfast.com/hp/dv2200.htm Hp dv2200 Battery http://www.batteryfast.com/hp/dv4000.htm Hp dv4000 Battery http://www.batteryfast.com/hp/dv6000.htm Hp dv6000 Battery http://www.batteryfast.com/ibm/thinkpad-t40.htm Ibm thinkpad t40 Battery http://www.batteryfast.com/toshiba/satellite-a105.htm Toshiba satellite a105 Battery http://www.batteryfast.com/toshiba/satellite-m45.htm Toshiba satellite m45 Battery http://www.batteryfast.com/toshiba/pa3285u-3bas.htm Toshiba pa3285u-3bas Battery http://www.batteryfast.com/toshiba/pa3395u-1brs.htm Toshiba pa3395u-1brs Battery http://www.batteryfast.com/toshiba/pa3399u-1bas.htm Toshiba pa3399u-1bas Battery http://www.batteryfast.com/toshiba/pa3399u-1brs.htm Toshiba pa3399u-1brs Battery http://www.batteryfast.com/toshiba/pa3399u-2bas.htm Toshiba pa3399u-2bas Battery http://www.batteryfast.com/toshiba/pa3399u-2brs.htm Toshiba pa3399u-2brs Battery http://www.batteryfast.com laptop battery http://www.batteryfast.com laptop batteries http://www.batteryfast.com/toshiba/pa3465u-1brs.htm Toshiba pa3465u-1brs Battery http://www.batteryfast.com/toshiba/pa3534u-1brs.htm Toshiba pa3534u-1brs Battery http://www.batteryfast.com/toshiba/pa3594u-1brs.htm Toshiba pa3594u-1brs Battery http://www.batteryfast.com/uniwill/255-3s4400-g1l1.htm Uniwill 255-3s4400-g1l1 Battery http://www.batteryfast.com/uniwill/258-4s4400-s1p1.htm Uniwill 258-4s4400-s1p1 Battery http://www.batteryfast.com/hp/pavilion-dv9000.htm HP pavilion dv9000 Battery http://www.batteryfast.com/hp/pavilion-dv9700.htm HP pavilion dv9700 Battery http://www.batteryfast.com/hp/pavilion-dv9500.htm HP pavilion dv9500 Battery http://www.batteryfast.com/hp/pavilion-dv9600.htm HP pavilion dv9600 Battery http://www.batteryfast.com/dell/inspiron-b130.htm Dell inspiron b130 Battery http://www.batteryfast.com/dell/inspiron-mini-9.htm Dell inspiron mini 9 Battery http://www.batteryfast.com/hp/nc8230.htm Hp nc8230 Battery http://www.batteryfast.com/hp/nc8430.htm Hp nc8430 Battery http://www.batteryfast.com/hp/nw8000.htm Hp nw8000 Battery http://www.batteryfast.com/hp/nw8200.htm Hp nw8200 Battery http://www.batteryfast.com/hp/nc6000.htm Hp nc6200 Battery http://www.batteryfast.com/hp/nc6100.htm Hp nc6100 Battery http://www.batteryfast.com/hp/nc6120.htm Hp nc6120 Battery http://www.batteryfast.com/hp/nc6400.htm Hp nc6400 Battery http://www.batteryfast.com/hp/pavilion-dv2000.htm Hp pavilion dv2000 Battery http://www.batteryfast.com/hp/pavilion-dv6000.htm Hp pavilion dv6000 Battery http://www.batteryfast.com/hp/pavilion-dv8000.htm Hp pavilion dv8000 Battery http://www.batteryfast.com/hp/pavilion-dv9000.htm Hp pavilion dv9000 Battery http://www.batteryfast.com/hp/pavilion-dv9500.htm Hp pavilion dv9500 Battery http://www.batteryfast.com/hp/pavilion-dv9600.htm Hp pavilion dv9600 Battery http://www.batteryfast.com/hp/pavilion-dv9700.htm Hp pavilion dv9700 Battery http://www.batteryfast.com/laptop-ac-adapter/acer/aspire-5520.htm Acer aspire 5520 Adapter http://www.batteryfast.com/laptop-ac-adapter/acer/aspire-5920.htm Acer aspire 5920 Adapter http://www.batteryfast.com/dell/kd476.htm Dell kd476 Battery http://www.batteryfast.com/dell/kr-onx511.htm Dell kr-onx511 Battery http://www.batteryfast.com/dell/latitude-d620.htm Dell latitude d620 Battery http://www.batteryfast.com/dell/latitude-d630.htm Dell latitude d630 Battery

  2. Unknown says:

    http://www.batteryfast.com/asus/f3.htm Asus f3 Battery http://www.batteryfast.com/toshiba/pa3594u-1brs.htm Toshiba pa3594u-1brs Battery http://www.batteryfast.com/toshiba/pa3593u-1bas.htm Toshiba pa3593u-1bas Battery http://www.batteryfast.com/toshiba/pabas111.htm Toshiba pabas111 Battery http://www.batteryfast.com/dell/xps-m1530.htm Dell xps m1530 Battery http://www.batteryfast.com/hp/tx2000.htm Hp tx2000 Battery http://www.batteryfast.com/hp/pavilion-tx1000.htm Hp pavilion tx1000 Battery http://www.batteryfast.com/dell/xps-m1730.htm Dell xps m1730 Battery http://www.batteryfast.com/hp/pavilion-dv6.htm Hp pavilion dv6 Battery http://www.batteryfast.com/toshiba/pa3395u-1brs.htm Toshiba pa3395u-1brs Battery http://www.batteryfast.com/toshiba/pa3421u-1brs.htm Toshiba pa3421u-1brs Battery http://www.batteryfast.com/toshiba/pa3451u-1brs.htm Toshiba pa3451u-1brs Battery http://www.batteryfast.com/toshiba/pa3399u-1brs.htm Toshiba pa3399u-1brs Battery http://www.batteryfast.com/toshiba/pa3399u-2bas.htm Toshiba pa3399u-2bas Battery http://www.batteryfast.com/toshiba/satellite-a100.htm Toshiba satellite a100 Battery http://www.batteryfast.com/toshiba/satellite-m40.htm Toshiba satellite m40 Battery http://www.batteryfast.com/toshiba/satellite-m45.htm Toshiba satellite m45 Battery http://www.batteryfast.com/toshiba/satellite-m55.htm Toshiba satellite m55 Battery http://www.batteryfast.com/toshiba/pa3356u.htm Toshiba pa3356u Battery http://www.batteryfast.com/toshiba/pa3356u-1bas.htm Toshiba pa3356u-1bas Battery http://www.batteryfast.com/toshiba/pa3356u-1brs.htm Toshiba pa3356u-1brs Battery http://www.batteryfast.com/toshiba/pa3356u-2brs.htm Toshiba pa3356u-2brs Battery http://www.batteryfast.com/toshiba/pa3456u-1brs.htm Toshiba pa3456u-1brs Battery http://www.batteryfast.com/toshiba/portege-m500.htm Toshiba portege m500 Battery http://www.batteryfast.com/toshiba/pa3451u.htm Toshiba pa3451u Battery http://www.batteryfast.com/toshiba/pabas067.htm Toshiba pabas067 Battery http://www.batteryfast.com/toshiba/pa3465u-1brs.htm Toshiba pa3465u-1brs Battery http://www.batteryfast.com/toshiba/pa3534u-1brs.htm Toshiba pa3534u-1brs Battery http://www.batteryfast.com/toshiba/pa3533u-1bas.htm Toshiba pa3533u-1bas Battery http://www.batteryfast.com/toshiba/satellite-a205.htm Toshiba satellite a205 Battery http://www.batteryfast.com/toshiba/pa3534u-1bas.htm Toshiba pa3534u-1bas Battery http://www.batteryfast.com/uniwill/255-3s4400-g1l1.htm Uniwill 255-3s4400-g1l1 Battery http://www.batteryfast.com/uniwill/un255.htm Uniwill un255 Battery

  3. Unknown says:

    http://www.shopgogo.ca/acer/aspire-3020.htm acer aspire 3020 battery , http://www.shopgogo.ca/acer/aspire-3610.htm acer aspire 3610 battery , http://www.shopgogo.ca/acer/squ-401.htm acer squ-401 battery , http://www.shopgogo.ca/acer/lcbtp03003.htm acer lcbtp03003 battery , http://www.shopgogo.ca/acer/travelmate-2300.htm acer travelmate 2300 battery , http://www.shopgogo.ca/acer/travelmate-4000.htm acer travelmate 4000 battery , http://www.shopgogo.ca/acer/travelmate-3002wtci.htm acer travelmate 3002wtci battery , http://www.shopgogo.ca/acer/travelmate-3002wtmi.htm acer travelmate 3002wtmi battery , http://www.shopgogo.ca/acer/travelmate-3200.htm acer travelmate 3200 battery , http://www.shopgogo.ca/acer/lip6179qupcsy6.htm acer lip6179qupcsy6 battery , http://www.shopgogo.ca/acer/batbl50l6.htm acer batbl50l6 battery , http://www.shopgogo.ca/acer/travelmate-4200.htm acer travelmate 4200 battery , http://www.shopgogo.ca/acer/aspire-5100.htm acer aspire 5100 battery , http://www.shopgogo.ca/acer/aspire-3690.htm acer aspire 3690 battery , http://www.shopgogo.ca/acer/aspire-5680.htm acer aspire 5680 battery , http://www.shopgogo.ca/acer/aspire-9120.htm acer aspire 9120 battery , http://www.shopgogo.ca/acer/um08a71.htm acer um08a71 battery , http://www.shopgogo.ca/acer/black-aspire-one-a110l-series.htm acer black aspire one a110l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150l-series.htm acer black aspire one a150l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150x-series.htm acer black aspire one a150x series battery , http://www.shopgogo.ca/acer/black-aspire-one-zg5.htm acer black aspire one zg5 battery , http://www.shopgogo.ca/acer/um08a72.htm acer um08a72 battery , http://www.shopgogo.ca/acer/blue-aspire-one-a110l-series.htm acer blue aspire one a110l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150l-series.htm acer blue aspire one a150l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150x-series.htm acer blue aspire one a150x series battery , http://www.shopgogo.ca/acer/blue-aspire-one-zg5.htm acer blue aspire one zg5 battery , http://www.shopgogo.ca/acer/um08a73.htm acer um08a73 battery , http://www.shopgogo.ca/acer/white-aspire-one-a110l-series.htm acer white aspire one a110l series battery , http://www.shopgogo.ca/acer/white-aspire-one-a150l-series.htm acer white aspire one a150l series battery , http://www.shopgogo.ca/acer/white-aspire-one-a150x-series.htm acer white aspire one a150x series battery , http://www.shopgogo.ca/acer/white-aspire-one-zg5.htm acer white aspire one zg5 battery , http://www.shopgogo.ca/acer/um08a74.htm acer um08a74 battery , http://www.shopgogo.ca/acer/black-aspire-one-a110l-series.htm acer black aspire one a110l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150l-series.htm acer black aspire one a150l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150x-series.htm acer black aspire one a150x series battery , http://www.shopgogo.ca/acer/black-aspire-one-zg5.htm acer black aspire one zg5 battery , http://www.shopgogo.ca/acer/um08b72.htm acer um08b72 battery , http://www.shopgogo.ca/acer/blue-aspire-one-a110l-series.htm acer blue aspire one a110l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150l-series.htm acer blue aspire one a150l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150x-series.htm acer blue aspire one a150x series battery , http://www.shopgogo.ca/acer/blue-aspire-one-zg5.htm acer blue aspire one zg5 battery , http://www.shopgogo.ca/acer/um08b73.htm acer um08b73 battery , http://www.shopgogo.ca/acer/white-aspire-one-a110l-series.htm acer white aspire one a110l series battery , http://www.shopgogo.ca/acer/white-aspire-one-a150l-series.htm acer white aspire one a150l series battery , http://www.shopgogo.ca/acer/btp-63d1.htm acer btp-63d1 battery ,

  4. Unknown says:

    Serena Williams and the season of her contentwow gold,world of warcraft gold,buy wow gold,wow power leveling,cheap wow gold,world of warcraft power
    leveling,world of warcraft gold,buy wow
    gold,ffxi gil,buy
    wow gold,wow power
    leveling,ffxi gil,world
    of warcraft power leveling,sell wow gold,wow power
    level,wow gold for
    sale,power leveling,wow
    power level,wow power level,power leveling,wow gold for
    sale,buy cheap wow gold.Indian brides confront gold bears, again

  5. William says:

    Here is one of many links on the subject as to why DCL is broke. They do it justice more then I. The end result is use a lock first or the static pattern above:http://www-106.ibm.com/developerworks/java/library/j-dcl.html?dwzone=javaCheers!–William

  6. Martin says:

    Hi William, why double checking pattern will not work in this case?

Comments are closed.