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

Kamil Skalski nazgul at nemerle.org
Mon Oct 25 17:40:47 EDT 2004


Dnia poniedziałek, 25 października 2004 23:29, gennady wexler napisał:
> 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;
>   }
> }

It does not matter. lock statement is translated to
      System.Threading.Monitor.Enter ($x);
      try {
        $body
      } finally {
        System.Threading.Monitor.Exit ($x);
      }

So lock is released automatically before return.


-- 
Kamil Skalski
http://nemerle.org developer



More information about the Mono-devel-list mailing list