Loading Revolution ...
Z Talk - Beta
Talk Revolution ...
G-TalkYahoo!MSN
Discussion Forum:
    Forum Overview
    Thread Administration
    Topic Administration
    Hit it Now ...
    View Topics/Threads

General Forums:
    Fast Forwards
    IT Forum
    My Voice ...

Technical Forums:
    MSDN:C# forum
    Asp.Net Forum
    c#.Net Forum
    VB.Net Forum
    VB6 Forum
    MS-SQL Forum
    SUN Java/Jsp
    C/C++/Linux/Unix
   You are Here:   Skip Navigation LinksHome > Brainstorming Forums > > c#.Net Forum        Make This My Home Page
 
No messages were posted in the selected interval. You might like to contact Administrator to start a new blog/thread/post. Or, try looking @ Archives .Here is the last post:

Wednesday, July 16

Singleton Design Pattern and ReaderWriterLock Class in CLR via C# - Design Pattern Examples  @ 12:25 AM   

Last night as I was installing software for a course, I grabbed Jeffrey Richter's new book, called CLR via C#. This is a follow-up to his last book, Applied Microsoft.NET Framework Programming, which is one of my all-time favorite books. The book looks similar in content and structure, but one can hopefully assume it has been updated for .NET 2.0 :) I haven't had a chance to dig into yet, so I can't be sure of all the differences and new content.

Singleton Design Pattern
As I thumbed through the book, I decided to read the section on Thread Synchronization, which talked about the Singleton Design Pattern and the ReaderWriterLock Class. The Singleton Design Pattern is one of those often talk about design patterns, but I rarely use it. The book mentions the famous Double-Check Locking Technique used for thread synchronization:

public sealed class Singleton
{
private static Singleton _instance;
private static object syncLock = new object();

private Singleton() {}

public static Singleton Instance {
get {
if (_instance == null) {
lock(syncLock) {
if (_instance == null)
{
_instance = new Singleton();
}
}
}

return _instance;
}
}
}

I changed up the code a bit on-the-fly from Jeffrey's example in the book and removed his comments, but this is essentially the famous double-check locking technique used to create singletons and assure thread synchronization.

I have been using a different technique where I instantiate the singleton in the class constructor, which is guaranteed to only run once and be thread safe. This eliminates all that locking code:

public sealed class Singleton
{
private static Singleton _instance;

static Singleton() {
_instance = new Singleton();
}

private Singleton() {}

public static Singleton Instance {
get {
return _instance;
}
}
}


It was cool to see that Jeffrey also prefers a variation of what I use. Basically the same code, he just eliminates the constructor call and instantiates the object directly, which is fine if you don't have any additional processing.

public sealed class Singleton
{
private static Singleton _instance = new Singleton();

private Singleton() {}

public static Singleton Instance {
get {
return _instance;
}
}
}

The advantage of the double-check locking technique is essentially lazy-loading. In the other methods, one doesn't know for sure when the class constructor will be called, only that it will be called before an object of the type is instantiated. Hence, the singleton instance could be instantiated even if the client doesn't request it. However, unless the singleton creation is memory and process intensive and there is a good chance it might not be used, this is hardly a big deal.


ReaderWriterLock Class
Jeffrey begins to talk about the ReaderWriterLock Class, which I have seen used but never used it myself. What was interesting, is that Jeffrey isn't too fond of this class. I think he likes the idea of it, but has a problem with its implementation. Here is a piece from the book:

“Normally, at this point in the book, I'd describe how to use this lock in your own applications; however, I just can't do that here because I don't recommend that anyone ever use it. As you will see, this class has many problems with its implementation.“ - CLR via C#, Richter, page 642.

Now my interest is peaked and I need to look into this class a bit more as I have seen entire MSDN Magazine articles on the ReaderWriterLock Class.

Post your own comment
0 comment(s):-


Copyright © 2007-08 Zhatak.com.All Rights Reserved. Hit counter
Select interval

Select a single day, a week or the entire month:

<October 2008>
>>SuMoTuWeThFrSa
>2829301234
>567891011
>12131415161718
>19202122232425
>2627282930311
>2345678

(Set the focus to the textbox to show the calendar > Click on arrow or month or year to select dates)
Other interval:
From:
To:
Archives
2008 - July (1)
2008 - June (5)
2008 - April (2)
2008 - March (1)
2008 - February (2)
2007 - November (2)
  •  
  • Maharashtra to legalise live-in relationships
  •  
  • No need to hit panic button: Counsellors
  •  
  • You are in queue, software majors tell new recruits
  •  
  • Economic slowdown to change campus placement trends
  •  
  • 38 days later, Orissa govt admits nun`s rape
  •  
  • After banks and nations, Iceland close to bankruptcy
  •  
  • Death of the American Dream: Indian kills family of five, self in LA
  •  
  • India top spam originator in Asia
  •  
  • Gmail gets Google Goggles
  •  
  • Gmail gets Google Goggles
  •  
  • TCS picks up Citigroup`s BPO arm for $505m
  •  
  • Sweet revenge for Gujarat
  •  
  • Bush signs Indo-US nuclear bill
  •  
  • Hospital ordered to pay Rs 1 lakh for needless tests
  •  
  • Do not forget the real issues
  •  
  • Indian-American given job of saving Wall Street
  •  
  • Making A Mockery Of Jihad
  •  
  • Mansoor Peerbhoy: An unlikely jihadi, he shows no remorse
  •  
  • When everyone lost
  •  
  • Pains of a slowing economy
  •  
  • Indian IT giants bidding for British tech consultancy: Report
  •  
  • Part-time vegetarianism takes root ..Flexitarians ??
  •  
  • Microsoft reviews hiring, to add jobs
  •  
  • BJP wants good ties with US: Advani to Rice
  •  
  • Nano - which state will host?
  •  
  • Day after pullout, trouble breaks out in Singur
  •  
  • Speak out and say yes to unity
  •  
  • 4.2 million new `green` jobs possible
  •  
  • English-speaking beggars on prowl in Delhi streets
  •  
  • Finally Done
  •  
  • Smoky Debate
  •  
  • Terror, inflation may dampen N-celebrations
  •  
  • Nuclear deal to be a win-win for India, US
  •  
  • Soumya murder has working women jittery
  •  
  • Google unveils $4.4 trillion "Clean Power by 2030" plan
  •  
  • Break It Up
  •  
  • Home truths about `those homos`
  •  
  • The Pirate Threat
  •  
  • Losing my religion
  •  
  • Ten Best tech Employers
  •  
  • Financial planning for India and indians
  •  
  • Stay Hungry Stay Foolish: 25 IIM-A entrepreneurs profiled by Rashmi Bansal
  •  
  • Is India ready for my.barackobama like political platform ?
  •  
  • Are you appalled by Preity Zinta endorsing BSNL
  •  
  • Power guzzling yellow lighting is going away in Haryana
  •  
  • Missing in Action
  •  
  • Indian democracy is doing fine
  •  
  • Now, TCS postpones promotions
  •  
  • Nuke deal caught in US financial storm
  •  
  • Dismissed employees beat CEO to death
  •  
  • India ranks 77th in Economic Freedom Index: Report
  •  
  • Is America becoming socialist?
  •  
  • Fragrance of fire
  •  
  • Free education for all up to XII: Raje
  •  
  • TCS, Satyam defer joining dates
  •  
  • Indian developers
  •  
  • Wanted: Housewives for banking, IT, retail jobs
  •  
  • RETAIL RIP-OFF: Happens Only In India
  •  
  • What is a B-school grad to do when Wall Street stops hiring?
  •  
  • They All Fall Down :First Lehman, then Merrill Lynch. What is next?
  •  
  • IT Pink slips age is back!!!!
  •  
  • TCS taking BT stake in Tech Mahindra?
  •  
  • Blind love or blind justice??
  •  
  • Mamata is next nuclear hurdle
  •  
  • The one-sixth sense
  •  
  • Eating veggies shrinks the brain
  •