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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:36:35+00:00 2026-05-26T18:36:35+00:00

I have now a scenario that needs to add and remove items in multi-threading

  • 0

I have now a scenario that needs to add and remove items in multi-threading condition

I’m doing

lock(list)
{
    if(!list.Contains(item))
    {
        list.Add(Item);
    }
}

and

lock(list)
{
    if(list.Contains(item))
    {
        list.Remove(Item);
    }
}

But I have now a very big problem with race condition. The lock time is increasing and incrasing.

I wished I could use ConcurrentBag but it doesn’t has Contains Method, so I can’t remove the specific item I want to remove.

I’m now using ConcurrentDicionary as a temporary solution. but it’s definitely not the proper way to do it.

So my question is how can I solve this problem? Is there any lock free implementation for things like this out there? Because none of the Collections under Concurrent name space suit for this problem.

  • 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-26T18:36:35+00:00Added an answer on May 26, 2026 at 6:36 pm

    What you want is a concurrent version of HashSet<T>. ConcurrentBag<T> wasn’t meant for that, it’s for support of efficient producer-consumer scenarios. There isn’t any such collection in the Framework.

    So, you could use ConcurrentDictionary<Item, Item>, as you said. But I agree with you that it’s not an ideal solution.

    Or, you could replace your locking code that uses List<T> with one that uses HashSet<T>. Operations like Contains() and Remove() can be very slow on large lists. But they should be fast in hash sets, which should improve your performance significantly, if your collection is large enough.

    It’s also possible there is some third-party library that contains something like ConcurrentHashSet<T>, but I don’t know about any.

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

Sidebar

Related Questions

I have now 1gb ram at work(i hope that will change soon) and jboss
I have now worked on two different teams that use the Agile/Scrum approach in
I have a scenario that i suspect is very common, i've found various ideas
Scenario: I have several services that I want to be discovered by different clients.
I have now seen 2 methods for determining if an argument has been passed
I have now witnessed two companies move to agile development with Scrum. In both
What I have now (which successfully loads the plug-in) is this: Assembly myDLL =
I have cleaned up my site and discarded lots of pages. I have now
Where I am: Linux command line The problem I have now: Sometimes I couldn't
This problem has bugged me so many times and i have now decided to

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.