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

  • Home
  • SEARCH
  • 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 7840101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:39:33+00:00 2026-06-02T15:39:33+00:00

I have a list that is accessed by multiple background threads to update/read. Updates

  • 0

I have a list that is accessed by multiple background threads to update/read. Updates actions include both insertions and deletions.

To do this concurrently without synchronization problems, I am using a lock on a private readonly object in the class.

To minimize the time I need to lock the list when reading its data, I do a deep clone of it and return the deep clone and unlock the dictionary for insert/delete updates.

Due to this every read of the list increases the memory consumption of my service.

One point to note is that the inserts/deletes are internal to the class that contains the list. But the read is meant for public consumption.

My question is:

Is there any way, I can avoid cloning the list and still use it concurrently for reads using read/write locks?

public class ServiceCache
    {
        private static List<Users> activeUsers;
        private static readonly object lockObject = new object();
        private static ServiceCache instance = new ServiceCache();

        public static ServiceCache Instance
        {
            get
            {
                return instance;
            }
        }

        private void AddUser(User newUser)
        {
            lock (lockObject)
            {
                //... add user logic
            }
        }

        private void RemoveUser(User currentUser)
        {
            lock (lockObject)
            {
                //... remove user logic
            }
        }

        public List<Users> ActiveUsers
        {
            get
            {
                lock (lockObject)
                {
                    //The cache returns deep copies of the users it holds, not links to the actual data.
                    return activeUsers.Select(au => au.DeepCopy()).ToList();
                }
            }
        }
    }
  • 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-02T15:39:35+00:00Added an answer on June 2, 2026 at 3:39 pm

    It sounds like you need to use the ConcurrentDictionary class, and create a key for each of the Users objects you are storing. Then it becomes as simple as this for adding / updating a user:

    _dictionary.AddOrUpdate("key", (k, v) =>
        {
            return newUser;
        }, (k, v) =>
        {
            return newUser;
        });
    

    And then for removing, you would do this:

     Users value = null;
    _dictionary.TryRemove("key", out value);
    

    Getting the list of people would be super easy as well, since you would just need to do:

    return _dictionary.Values.Select(x => x.Value).ToList();
    

    Which should return a copy of the dictionary contents at that very moment.

    And let the .NET runtime take care of the threading for you.

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

Sidebar

Related Questions

I have a List object being accessed by multiple threads. There is mostly one
I am using a ConcurrentSkipListSet, that is obviously accessed through multiple threads. Now, the
I have a list function that can be accessed like this: _list/characters/characters_by_user?startkey=[org.couchdb.user:rodriguez]&endkey=[org.couchdb.user:rodriguez, {}] I'm
in my program, i have a list of lists that needs to be accessed
I have a java class that is accessed by a lot of threads at
I have a List containing elements that are accessed according to their indexes. In
I have a List that contains items, for example: 1) https:\\10.12.23\\ 2) https:\\12.23.12.25\\ 3)
I have one list that I want to take a slice of, reverse that
I have a list that I need to send through a URL to a
I have a list that has some chapter numbers in string. When I sort

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.