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

Andriy G. Tereshchenko mono-list at spam.24.odessa.ua
Mon Oct 25 17:38:27 EDT 2004


At first - locking on Types is not good. 

As second - return statement inside lock region or outside - there is almost no difference - except that your return statement can
possibly return value assigned to variable from different thread ;-) 

As third  - this thread completely on different topic - it's all about difference in Mono vs. Microsoft in implementation of type
constructors. 

bash-2.03$ cat > EntryPoint.cs

using System;

public class Singleton {
private Singleton() { throw new ArgumentException("Configuration is invalid");}
static Singleton() {}
public static Singleton Instance = new Singleton();
}
public class EntryPoint {
 public static void Main(String[] args) {
   try {
       Console.WriteLine("First attempt");
       Singleton s = Singleton.Instance;
   } catch(Exception e) { Console.WriteLine(e); }
   try {
       Console.WriteLine("Second attempt");
       Singleton s = Singleton.Instance;
       if(s==null) Console.WriteLine("Mono possibly wrong !!");
   } catch(Exception e) { Console.WriteLine(e); }  }
}
bash-2.03$
bash-2.03$ mcs EntryPoint.cs
Compilation succeeded
bash-2.03$ mono EntryPoint.exe
First attempt
System.TypeInitializationException: An exception was thrown by the type initiali
zer for Singleton ---> System.ArgumentException: Configuration is invalid
in <0x00036> Singleton:.ctor ()
in <0x0001d> Singleton:.cctor ()
--- End of inner exception stack trace ---

in (unmanaged) EntryPoint:Main (string[])
in <0x00034> EntryPoint:Main (string[])

Second attempt
Mono possibly wrong !!

bash-2.03$ mono --version
Mono JIT compiler version 1.1.1, (C) 2002-2004 Novell, Inc and Contributors. www
.go-mono.com
        TLS:           normal
        GC:            Included Boehm (with typed GC)
        SIGSEGV      : normal
        Globalization: ICU
bash-2.03$ uname -a
SunOS tigger 5.8 Generic_117351-03 i86pc i386 i86pc


While Microsoft:
Microsoft (R) Visual C# .NET Compiler version 7.10.4156.4
for Microsoft (R) .NET Framework version 1.1.4322

First attempt
System.TypeInitializationException: The type initializer for "Singleton" threw an exception. ---> System.ArgumentException:
Configuration is invalid
   at Singleton..ctor()
   at Singleton..cctor()
   --- End of inner exception stack trace ---
   at EntryPoint.Main(String[] args)
Second attempt
System.TypeInitializationException: The type initializer for "Singleton" threw an exception. ---> System.ArgumentException:
Configuration is invalid
   at Singleton..ctor()
   at Singleton..cctor()
   --- End of inner exception stack trace ---
   at EntryPoint.Main(String[] args)


Feel the difference ! 

--
Andriy G. Tereshchenko
Odessa, Ukraine 

> -----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: Tuesday, October 26, 2004 12:02 AM
> 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