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 328617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:30:43+00:00 2026-05-12T09:30:43+00:00

I’m very new to multi-threading and for some reason this class is giving me

  • 0

I’m very new to multi-threading and for some reason this class is giving me more trouble than it should.

I am setting up a dictionary in the ASP.net cache – It will be frequently queried for individual objects, enumerated occasionally, and written extremely infrequently. I’ll note that the dictionary data is almost never changed, I’m planning on letting it expire daily with a callback to rebuild from the database when it leaves the cache.

I believe that the enumeration and access by keys are safe so long as the dictionary isn’t being written. I’m thinking a ReaderWriterLockSlim based wrapper class is the way to go but I’m fuzzy on a few points.

If I use Lock I believe that I can either lock on a token or the actual object I’m protecting. I don’t see how to do something similar using the ReaderWriter Lock. Am I correct in thinking that multiple instances of my wrapper will not lock properly as the ReaderWriterLocks are out of each other’s scope?

What is the best practice for writing a wrapper like this? Building it as a static almost seems redundant as the primary object is being maintained by the cache. Singleton’s seem to be frowned upon, and I’m concerned about the above mentioned scoping issues for individual instances.

I’ve seen a few implementations of similar wrappers around but I haven’t been able to answer these questions. I just want to make sure that I have a firm grasp on what I’m doing rather than cutting & pasting my way through. Thank you very much for your help!


**Edit: Hopefully this is a clearer summary of what I’m trying to find out- **

1. Am I correct in thinking that the lock does not affect the underlying data and is scoped like any other variable?

As an example lets say i have the following –

MyWrapperClass 
{
    ReaderWriterLockSlim lck = new ReaderWriterLockSlim();
    Do stuff with this lock on the underlying cached dictionary object...
}

MyWrapperClass wrapA = new MyWrapperClass();
MyWrapperClass wrapB = new MyWrapperClass();

Am I right in thinking that the wrapA lock and wrapB lock won’t interact, And that if wrapA & wrapB both attempt operations it will be unsafe?

2. If this is the case what is the best practice way to “share” the lock data?

This is an Asp.net app – there will be multiple pages that need to access the data which is why i’m doing this in the first place. What is the best practice for ensuring that the various wrappers are using the same lock? Should my wrapper be a static or singleton that all threads are using, if not what is the more elegant alternative?

  • 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-12T09:30:43+00:00Added an answer on May 12, 2026 at 9:30 am

    You have multiple dictionary objects in the Cache, and you want each one locked independently. The “best” way is to just use a simple class that does it for you.

    public class ReadWriteDictionary<K,V>
    {
        private readonly Dictionary<K,V> dict = new Dictionary<K,V>();
        private readonly ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();
    
        public V Get(K key)
        {
            return ReadLock(() => dict[key]);
        }
    
        public void Set(K key, V value)
        {
            WriteLock(() => dict.Add(key, value));
        }
    
        public IEnumerable<KeyValuePair<K, V>> GetPairs()
        {
            return ReadLock(() => dict.ToList());
        }
    
        private V2 ReadLock<V2>(Func<V2> func)
        {
            rwLock.EnterReadLock();
            try
            {
                return func();
            }
            finally
            {
                rwLock.ExitReadLock();
            }
        }
    
        private void WriteLock(Action action)
        {
            rwLock.EnterWriteLock();
            try
            {
                action();
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
        }
    }
    
    Cache["somekey"] = new ReadWriteDictionary<string,int>();
    

    There is also a more complete example on the help page of ReaderWriterLockSlim on MSDN. It wouldn’t be hard to make it generic.

    edit To answer your new questions –

    1.) You are correct wrapA and wrapB will not interact. They both have their own instance of ReaderWriterLockSlim.

    2.) If you need a shared lock amongst all your wrapper classes, then it must be static.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.