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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:08:04+00:00 2026-05-26T09:08:04+00:00

I have List<string> collection called List<string> list . I have two threads. One thread

  • 0

I have List<string> collection called List<string> list.

I have two threads.
One thread is enumerating through all list elements and adding to collection.
Second thread is enumerating through all list elements and removing from it.

How can make it thread safe?
I tried creating global Object “MyLock” and using lock(MyLock) block in each thread function but it didn’t work.

Can you help me?

  • 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-26T09:08:05+00:00Added an answer on May 26, 2026 at 9:08 am

    If you have access to .NET 4.0 you can use the class ConcurrentQueue or a BlockingCollection with a ConcurrentQueue backing it. It does exactly what you are trying to do and does not require any locking. The BlockingCollection will make your thread wait if there is no items available in the list.

    A example of removing from the ConcurrentQueue you do something like

    ConcurrentQueue<MyClass> cq = new ConcurrentQueue<MyClass>();
    
    void GetStuff()
    {
        MyClass item;
        if(cq.TryDeqeue(out item))
        {
            //Work with item
        }
    }
    

    This will try to remove a item, but if there are none available it does nothing.

    BlockingCollection<MyClass> bc = BlockingCollection<MyClass>(new ConcurrentQueue<MyClass>());
    
    void GetStuff()
    {
        if(!bc.IsCompleated) //check to see if CompleatedAdding() was called and the list is empty.
        {
            try
            {
                MyClass item = bc.Take();
                //Work with item
            }
            catch (InvalidOpperationExecption)
            {
                //Take is marked as completed and is empty so there will be nothing to take
            }
        }
    }
    

    This will block and wait on the Take till there is something available to take from the list. Once you are done you can call CompleteAdding() and Take will throw a execption when the list becomes empty instead of blocking.

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

Sidebar

Related Questions

I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have 2 collections of caracter string ex: List<string> one = new List<string>; one.Add(a);
If I have List<String> text how can I create a sub-list of all continious
Let's say I have two tables one called Customers and one called CustomerAddresses. Then
i have a list of objects in a collection. Each object has a string
I have a C# list collection that I'm trying to sort. The strings that
I have a list string tag. I am trying to initialize a dictionary with
I have a list of string values that I want add to a hashtable
I have a String List with items like this Root Root/Item1 Root/Item2 Root/Item3/SubItem1 Root/Item3/SubItem2

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.