Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:10:27+00:00 2026-05-24T11:10:27+00:00

Is the double check lock pattern necessary when using ReaderWriterLockSlim ? Consider this scenario:

  • 0

Is the double check lock pattern necessary when using ReaderWriterLockSlim?

Consider this scenario: I have a dictionary. Things can be added to it. But things can’t be removed from it. When things are added, it can be a very expensive operation in terms of time (only hundreds of milliseconds, but still costly relative to the rest of the app) If I wanted to add something and it wasn’t already there, would there be anything to gain by:

  1. first acquiring a read lock, then checking for existence,
  2. then entering an upgradable read lock, and checking again,
  3. then entering a write lock if the item is still not present in the dictionary?

Something like the following:

void populateIfNotPresent( object thing )
{
        _lock.EnterReadLock( ) ;

        bool there = _dictionary.ContainsKey(thing);

        _lock.ExitReadLock( ) ;

        // Remember, the specs say nothing can be removed from this dictionary.
        if (!there)
        {
            _lock.EnterUpgradeableReadLock( ) ;

            try
            {
                if( !_dictionary.ContainsKey( thing ) )
                {
                    _lock.EnterWriteLock( ) ;
                    try
                    {
                        populate( thing ) ;
                    }
                    finally
                    {
                        _lock.ExitWriteLock( ) ;
                    }
                }
            }
            finally
            {
                _lock.ExitUpgradeableReadLock( ) ;
            }
        }
}

The docs say only one thread at a time can enter an upgradable read lock, but doesn’t stop any other threads from entering a read lock, so it appears that there is value in the double check lock.

What do you think? Is this overkill?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:10:28+00:00Added an answer on May 24, 2026 at 11:10 am

    The ReaderWriterLockSlim class (like any other reader-writer lock) is meant for a high number of reads compared to the number of writes.

    What you are doing is actually triple-checking, and it’s superfluous; you might as well enter an ungradable write lock. If the item exists, then exit the lock, otherwise, upgrade to a write lock.

    Your method indicates that the read is not providing any value here, since you have a good possibility of performing the write. Since the upgradable write won’t block any other reads, it shouldn’t kill you here.

    However, if this is the only place that you are doing reads/writes (or the majority take place here) then there’s a problem, your ratio of reads/writes isn’t high enough to warrant a read/write lock and you should look to some other synchronization method.

    That said, in the end, it’s all about testing the performance; if you’re going to optimize an implementation, you need to measure it’s current performance in order to make a comparison against, otherwise, it’s just premature optimization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the double-check idiom for lazy initialization of instance fields: // Item 71 in
I came across this article discussing why the double-check locking paradigm is broken in
How do I enable double-buffering of a control using C# (Windows forms)? I have
I want to check when the user double click on applictaion icon that no
I want to double buffer a custom control which contains buttons. I have tried
In c# double tmp = 3.0 * 0.05; tmp = 0.15000000000000002 This has to
I have a double value f and would like a way to nudge it
double r = 11.631; double theta = 21.4; In the debugger, these are shown
double TotalMinute=300.0 double TotalMinutesAdded=1378.0 double TotalMinute=300.0 double TotalMinutesAdded=1378.0 foreach(DataRow dr in ds.Tables[0].Rows) { //Add
When you double click on a class (in 'solution explorer')... if that class happens

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.