[Mono-devel-list] What to do with TypeInitialization in case of exceptions on second attempt to access class ? Singleton pattern

gennady wexler adyler at winisp.net
Mon Oct 25 17:29:36 EDT 2004


you need to exit lock() before you return, otherwise you lock in critical
section forever. 

perhaps like this?

public static Singleton Instance  {
  get {
    lock (typeof(Singleton)) {
      if  (Singleton.instance == null)
        Singleton.instance = new Singleton();
     }
     return Singleton.instance; 
  }
}

-----Original Message-----
From: mono-devel-list-admin at lists.ximian.com
[mailto:mono-devel-list-admin at lists.ximian.com] On Behalf Of Rafael Teixeira
Sent: Monday, October 25, 2004 2:02 PM
To: Andriy G. Tereshchenko
Cc: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-devel-list] What to do with TypeInitialization in case of
exceptions on second attempt to access class ? Singleton pattern

If you need it to be thread-safe surround with the appropriate guarding
code:

public static Singleton Instance  {
  get {
    lock (typeof(Singleton)) {
      if  (Singleton.instance == null)
        Singleton.instance = new Singleton();
      return Singleton.instance; 
    }
  }
}

On Mon, 25 Oct 2004 23:39:55 +0300, Andriy G. Tereshchenko
<mono-list at spam.24.odessa.ua> wrote:
> > Possibly Rafael Teixeira, possibly on Rafael Teixeira wrote possibly
this:
> >    public static Singleton Instance  {
> >      get {
> >        if  (Singleton.instance == null)
> >            Singleton.instance = new Singleton();
> >        return Singleton.instance; }
> >    }
> 
> This is not thread-safe. Period.
> 
> --
> 
> 
> Andriy G. Tereshchenko
> Odessa, Ukraine
> 
> 


--
Rafael "Monoman" Teixeira
---------------------------------------
Just the 'crazy' me in a sane world, or would it be the reverse? I dunno...
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list