singleton

Help on Ignacio's Design By Contract plug-in hosted on http://code.google.com/p/postsharp-user-plugins/.

singleton

Postby blahetal on Mon Mar 17, 2008 2:23 pm

Hi, is your implementation of Singleton thread-safe?
blahetal
blahetal
 
Posts: 13
Joined: Sat Jan 12, 2008 4:55 pm
Last Name: blahetal

Re: singleton

Postby cocotapioca on Mon Mar 17, 2008 4:20 pm

The current implementation use the static constructor to initialize the static variable that holds the singleton. Static constructors are called by the run-time at most once. So i believe they are thread-safe, but there is a lot to read with a simple google search about the issue. If someone can say a word about this is welcome to do so.

Cheers.
Ignacio
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
First Name: Ignacio
Last Name: Vivona

Re: singleton

Postby blahetal on Mon Mar 17, 2008 4:34 pm

Ok, I found this article
http://www.yoda.arachsys.com/csharp/singleton.html

and from your post I understand that you use the first method (to create a Singleton) that is described in the article above. Well, and there is written that using pure static constructors (without any synchronization mechanism) is not thread safe. Could you check it.
blahetal
blahetal
 
Posts: 13
Joined: Sat Jan 12, 2008 4:55 pm
Last Name: blahetal

Re: singleton

Postby cocotapioca on Mon Mar 17, 2008 5:50 pm

I have read that, but what i do is close to the fourth form. I do the initialization in the static constructor, the accessor only retrieves the instance.

Ignacio.
Ignacio Vivona
cocotapioca
 
Posts: 19
Joined: Sat Jan 12, 2008 4:55 pm
First Name: Ignacio
Last Name: Vivona


Return to DesignByContract