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:

<August 2008>
>>SuMoTuWeThFrSa
>272829303112
>3456789
>10111213141516
>17181920212223
>24252627282930
>31123456

(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)
  •  
  • We need federal police
  •  
  • If We Want To Win
  •  
  • Mera Bharat Kahan?
  •  
  • Independence Day for Kashmir
  •  
  • Out in the gold
  •  
  • Don't Fume & Fret
  •  
  • It is a PR exercise:HIV + job reservation in Kerala
  •  
  • Identity crisis
  •  
  • The world will love Mayawati as PM
  •  
  • Celebrating India's linguistic diversity
  •  
  • India welcomes golden boy Bindra
  •  
  • Why India is still a developing country
  •  
  • Thiz is Why I didnt take up GRE !!
  •  
  • Gold Hunters
  •  
  • What makes social networks unsafe
  •  
  • TCS, Infy, Wipro: Who topped Q1
  •  
  • Motorola appoints Indian as CEO
  •  
  • Tech giants get code of conduct
  •  
  • Ratan Tata not liable for TCS actions
  •  
  • A boy is preferred over a girl ..that's Indian sex ratio ..
  •  
  • I feel our whole fight was useless: Niketa Mehta
  •  
  • Racket Science ?? Was NASA story a fake?
  •  
  • India as nuclear exporter
  •  
  • MS, Yahoo to give Web tracking info
  •  
  • Patna IIT to start from August 6
  •  
  • Dosti ke side effects
  •  
  • Where Are India’s Innovative Companies, Products and Solutions?
  •  
  • Indian IT Companies and the Visa Story
  •  
  • Project to rebuild net gets $12M
  •  
  • Visiting US? Your laptop can be seized
  •  
  • The Pain is Global
  •  
  • Sting on BJP flops, Uma red-faced
  •  
  • From this year, 6 new IITs and 700 more seats
  •  
  • IIT physics cut-off down to zero as seats increase
  •  
  • Challenges of a new project manager in Indian IT company
  •  
  • People issues in Indian IT
  •  
  • Happy birthday...
  •  
  • India revs up for a dozen new small cars by 2010
  •  
  • Soon, you can touch 100kph on highways
  •  
  • India blames US for WTO talks failure
  •  
  • Tech behind job losses: Obama
  •  
  • Microsoft building Windows on Net
  •  
  • Do As The Americans Did
  •  
  • Keep the love alive!
  •  
  • Gujarati gymnast in US Olympic team
  •  
  • Slowdown creeps into hiring
  •  
  • Indian cos showering jobs in US
  •  
  • JEE fails to get the best: IIT dons
  •  
  • Why They Don't Behave
  •  
  • Now, docs in UK required to undergo yearly competence test
  •  
  • How to call off a love relationship
  •  
  • Men are tuned to lust, says study
  •  
  • Formula for finding true love!
  •  
  • Addicted to first love!
  •  
  • Go beyond Left and Right
  •  
  • Get real, we are living in a fake world
  •  
  • Keep the spark alive...