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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:59:49+00:00 2026-05-25T01:59:49+00:00

I’ve been asked to make dictionaries inside a class thread safe. My first proposal

  • 0

I’ve been asked to make dictionaries inside a class thread safe.

My first proposal was to implement a thread safe dictionary, the community of .net developers had already worked on that but it was rejected.

The code is something like this:

class Example()
{
    Dictionary<string, string> dic1;
    Dictionary<string, string> dic2;
     public void Example()
     {
        dic1 = new Dictionary<string,string>(10);
        dic2 = new Dictionary<string,string>(10);
     }

     public string Method1(string param1)
     {
            if(dic1.ContainsKey(param1))
            {
                return dic1[param1];
            }

            if(IsValidParam(param1))
            {
                dic1.Add(param1, param1);
                return param1;
            }

            try
            {   
                var params = GetValidParams(param1);
                if(params.Count > 0)
                {
                    foreach(var param in params)
                    {
                        if(!isValirParam(param)
                            continue;

                        dic1.Add(param1, param);

                        if(!dic2.ContainsKey(param1))
                        {
                            dic2.Add(param, param1);
                        }

                        return param;
                    }
                }
                else
                {
                    dic2.Add(param1, param1);
                    return param1;
                }
            }
            catch(Exception ex)
            {
                .....
            }

            return param1;
     }
}

This is only one of the many methods that have read and write access to both of the dictionaries inside the same method.

I was thinking on refactoring and use “ReaderWriterLockSlim” in each Add and return, but I don’t know if this is going to make this thread safe.

Do you have any idea how to approach this? I’m open to suggestions…

Thanks in advance for your time

  • 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-25T01:59:50+00:00Added an answer on May 25, 2026 at 1:59 am

    The simplest way in this case to ensure that you’re going to get what you expect is to use a lock.

    class Example()
    {
        Dictionary<string, string> dic1;
        Dictionary<string, string> dic2;
        private Object syncRoot;
         public void Example()
         {
            dic1 = new Dictionary<string,string>(10);
            dic2 = new Dictionary<string,string>(10);
            syncRoot = new Object();
         }
    
         public string Method1(string param1)
         {
             lock(syncRoot) {
                if(dic1.ContainsKey(param1))
                {
                    return dic1[param1];
                }
    
                if(IsValidParam(param1))
                {
                    dic1.Add(param1, param1);
                    return param1;
                }
    
                try
                {   
                    var params = GetValidParams(param1);
                    if(params.Count > 0)
                    {
                        foreach(var param in params)
                        {
                            if(!isValirParam(param)
                                continue;
    
                            dic1.Add(param1, param);
    
                            if(!dic2.ContainsKey(param1))
                            {
                                dic2.Add(param, param1);
                            }
    
                            return param;
                        }
                    }
                    else
                    {
                        dic2.Add(param1, param1);
                        return param1;
                    }
                }
                catch(Exception ex)
                {
                    .....
                }
    
                return param1;
              }
         }
    }
    

    Note that this will make things slower (lock has some overhead and in particular you won’t have two threads executing anything inside the lock’d block at the same time), but it ensures that Thread2 running this method can’t change something in between when Thread1 tested a value and when it then tries to use the result of that test to do something. It also doesn’t require .net 4, so you’ll be able to use it.

    edit – It’s also worth mentioning that if you have any other methods that modify either dictionary, you’ll want to lock them the same way. The key thing here is that only one thread can be messing around with stuff at any given moment of time.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.