Singleton and Thread Safe Methods on .Net

Español

Recently I had a code review on one of my colleagues project and found this common implementation there:

public sealed class TheClass {
   private TheClass() {}
   private static _theClass value;
   private static object obj = new Object();
    public static Instance Value {
          get {
                 if (TheClass.value == null) {
                        lock (syncRoot) {
                               if (TheClass.value == null) {
                                      TheClass.value = new TheClass();
                               }
                        }
                 }
                 return TheClass.value;
          }
   }
}

Yes we are talking about the classic Singleton pattern here, but not implemented as the proper or, more importantly, safer way. This implementation could permit a double instantation at some point. In short I’ll just recommend to make _theClass volatile, but I know it’s not the unique way to achieve this and is not always the safer way in all platforms. So searching this on the internet I found a lot of discussions so if you are interested on a deep level reading check this links:

volatile and MemoryBarriers
Double-checked locking in .net at Stackoverflow

Alois Kraus even shows the assembled code!
Lazy Vs Eager Init Singletons / Double-Check Lock Pattern

http://www.yoda.arachsys.com/csharp/singleton.html

And of course the Official Microsoft Pattern

Good reading!

Advertisements

Is UML Death?

I found this article just by accident. I don’t think that UML is Death because there is some concepts and things that almost everyone use from time to time like a sketch of some classes design or an interaction, but I agree on some of the very good topics denoted by

I’ll extract some:

  • Software design is not manufacturing no matter how much management likes to think about it this way.
  • Software creation is a creative activity so a more accurate description would be craft, art or something along this line.

Personally I think the big issue is the complexity. When UML was trying just to be a communication mechanism between the different stakeholders it was really good and simple but when got to many academic attention and revisions then become more and more complex and less useful. I think that UML is great when you use it as a simple communication mechanism and when you don’t try to express detailed technical details. If you need to express complex ideas there is a very common standard for that it is called written language and you can’t replace that with an impressive and big collection of symbols.

References:

http://littletutorials.com/2008/05/15/13-reasons-for-umls-descent-into-darkness/

Where is going Mono?

Last week I was reading some october interview with Joseph Hill. I don’t know very much about Mono, but i’m a little attracted to this effort and his philosophy. To give a try you can install it from here, and I had found a very old article here that give you an idea (warning: it’s very old and the code doesn’t work very well with latest versions).

I think that Microsoft is underestimating (again) the importance of the interoperability and open source with his own initiative. C# as a language, CLS and his CLR are a really good technologies. The freedom of language choice it’s excellent too, and they are great building productivity tools on top of that. MS don’t need to lock down a development framework. The CLR code implemented for Windows has it’s own customizations and has to work with MS Win32 current infrastructure. That code cannot be used as is on a Linux box or a Mac, and so on. I don’t see the advantage of MS locking his implementation and trying to intimidate others.

On the other hand I understand that Mono team are trying to catch up MS libraries on top of the CLR, (ASP.NET, Windows Forms, etc) because much of the code outside uses this libraries, but I think that isn’t the best in terms of interoperability and it’s a very tough effort (’cause of Win32 specific methods and properties).

What will be an ideal? Imagine a common CLR between MS and the open source community in terms of specifications, but with the open mind of sharing his own implementation for an excellent reference. Then specifications for common frameworks (desktop gui’s, web services api’s etc) you don’t need the source code here but the specification can’t be operating system specific (good bye Win32 Handlers in public methods or properties).

Even if they cannot share his implementations and cannot agree on common API’s like gui desktop management it’s not a good choice to re implement all MS specific API’s.

Maybe Mono team and his community have to promote real public specifications (on top of CLR of course) and promote the implementation for the most common operating systems out there.

C# it’s a very good and modern language, and CLR it’s the minimal infrastructure necessary for modern web and desktops applications today. I can’t think of a business application (not a real time system, etc) developed without events, garbage collector, serialization and configuration to name a few. It’s possible to do all of this with C++ if you have a lot of time and your customer pays all development in advance, but in the real life you’ll need all this included in your runtime.

Mono it’s more important than a MS technology ported to linux. Mono could be another good initiative to consolidate a good runtime and a very good language for open source or even for modern small ISV’s.