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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:12:27+00:00 2026-06-17T18:12:27+00:00

I have a Dictionary<object1, List<object2> (a abstractisation I made for my structure, which is

  • 0

I have a Dictionary<object1, List<object2> (a abstractisation I made for my structure, which is a little more complex).

The lists inside the dictionary are initialized in a non-parallel context, but adding new elements to the list needs a thread safe method. Removing items will not occure (the dictionary object will be disposed at the end of transactions).
Also, the key-value pairs are added only at initialisation, so no keys are added or removed from the dictionary during work, only the values are updated.

I can’t use concurent collections, I’m stuck in an old .NET Framework.

At first I locked the whole dictionary with a ReadWriteSlimlock. Well, this was very bad performance wise. There are many adding operations happening and they just wait for another. Locking only each list is a far better solution, because at least I’m doing stuff in parallel for each key.
Also, the adding operation are not simple list.Add(object2), some other complex operations need to occur in the thread safe area at adding.

But I don’t know what is the best way to implement it:

  1. lock(dictionary.Value) ?
  2. Using a dictionary of ReadWriteSlimlock (one for each key)?
  3. Any other better solution?
  • 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-06-17T18:12:29+00:00Added an answer on June 17, 2026 at 6:12 pm

    Another solution could be to implement you concurent List<object> class.
    Something like:

    public class ConcurentList {
        private object sync = new object(); 
        private List<object> realList = new List<object>(); 
    
    
        public void Add(object o) {
            lock(sync){
               realList.Add(o);
            }
        }   
    
        /** ADD OTHERE METHODS IMPEMENTATION IF NEED **/
    
    }
    

    and in dictionary have:

    Dictionary<object1, ConcurentList>
    

    Why encapsulate and and not extend List<object>, is cause Add method is not virtual, so the only way you can “override” it, is using new keyword, which is guranteed to be called only if used on the exactly same type, which means if you cast list to the base, it won’t be called, so hole architecture will fail.

    By encapsulating, you give to the caller only one method, where you control everything.

    Don’t know if this solution fits your needs, but hope it gives you some hints on how possibly manage stuff.

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

Sidebar

Related Questions

I have a list of objects which I need to store in a dictionary.
I have a dictionary of compound objects made up of a string, a list,
I have an object allStudents = Dictionary<ClassRoom, List<Student>>() In Linq how would I get
I have a dictionary object which i would like to encrypt, then put it
I have a Dictionary of objects I have created in smalltalk, which I am
I have a string data which I need to parse into a dictionary object.
I have an application that takes a dictionary of files (file type, and list
I have a dictionary<String,Object> and I want to convert it to a List<Customer> Is
Say I have a dictionary and then I have a list that contains the
I have a method which in essence converts a datatable to a list of

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.