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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:07:43+00:00 2026-06-13T22:07:43+00:00

I want to create a thread-safe collection that can be modified while being enumerated.

  • 0

I want to create a thread-safe collection that can be modified while being enumerated.

The sample ActionSet class stores Action handlers. It has the Add method that adds a new handler to the list and the Invoke method that enumerates and invokes all of the collected action handlers. The intended working scenarios include very frequent enumerations with occasional modifications while enumerating.

Normal collections throw exception if you modify them using the Add method while the enumeration is not over.

There is an easy, but slow solution to the problem: Just clone the collection before enumeration:

class ThreadSafeSlowActionSet {
    List<Action> _actions = new List<Action>();

    public void Add(Action action) {
        lock(_actions) {
            _actions.Add(action);
        }
    }

    public void Invoke() {
        lock(_actions) {
            List<Action> actionsClone = _actions.ToList();
        }
        foreach (var action in actionsClone ) {
            action();
        }
    }
}

The problem with this solution is the enumeration overhead and I want enumeration to be very fast.

I’ve created a rather fast “recursion-safe” collection that allows adding new values even while enumerating. If you add new values while the main _actions collection is being enumerated, the values are added to the temporary _delta collection instead of the main one. After all enumerations are finished, the _delta values are added to the _actions collection. If you add some new values while the main _actions collection is being enumerated (creating the _delta collection) and then re-enter the Invoke method again we have to create a new merged collection (_actions + _delta) and replace _actions with it.

So, this collection looks “recursion-safe”, but I want to make it thread-safe. I think that I need to use the Interlocked.* constructs, classes from System.Threading and other synchronization primitives to make this collection thread-safe, but I don’t have a good idea on how to do that.

How to make this collection thread-safe?

class RecursionSafeFastActionSet {
    List<Action> _actions = new List<Action>(); //The main store
    List<Action> _delta; //Temporary buffer for storing added values while the main store is being enumerated
    int _lock = 0; //The number of concurrent Invoke enumerations

    public void Add(Action action) {
        if (_lock == 0) { //_actions list is not being enumerated and can be modified
            _actions.Add(action);
        } else { //_actions list is being enumerated and cannot be modified
            if (_delta == null) {
                _delta = new List<Action>();
            }
            _delta.Add(action); //Storing the new values in the _delta buffer
        }
    }

    public void Invoke() {
        if (_delta != null) { //Re-entering Invoke after calling Add:  Invoke->Add,Invoke
            Debug.Assert(_lock > 0);
            var newActions = new List<Action>(_actions); //Creating a new list for merging delta
            newActions.AddRange(_delta); //Merging the delta
            _delta = null;
            _actions = newActions; //Replacing the original list (which is still being iterated)
        }
        _lock++;
        foreach (var action in _actions) {
            action();
        }
        _lock--;
        if (_lock == 0 && _delta != null) {
            _actions.AddRange(_delta); //Merging the delta
            _delta = null;
        }
    }
}

Update: Added the ThreadSafeSlowActionSet variant.

  • 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-13T22:07:44+00:00Added an answer on June 13, 2026 at 10:07 pm

    Here is your class modified for thread safety:

    class SafeActionSet
    {
        Object _sync = new Object();
        List<Action> _actions = new List<Action>(); //The main store
        List<Action> _delta = new List<Action>();   //Temporary buffer for storing added values while the main store is being enumerated
        int _lock = 0; //The number of concurrent Invoke enumerations
    
        public void Add(Action action)
        {
            lock(sync)
            {
                if (0 == _lock)
                { //_actions list is not being enumerated and can be modified
                    _actions.Add(action);
                }
                else
                { //_actions list is being enumerated and cannot be modified
                    _delta.Add(action); //Storing the new values in the _delta buffer
                }
            }
        }
    
        public void Invoke()
        {
            lock(sync)
            {
                if (0 < _delta.Count)
                { //Re-entering Invoke after calling Add:  Invoke->Add,Invoke
                    Debug.Assert(0 < _lock);
                    var newActions = new List<Action>(_actions); //Creating a new list for merging delta
                    newActions.AddRange(_delta); //Merging the delta
                    _delta.Clear();
                    _actions = newActions; //Replacing the original list (which is still being iterated)
                }
                ++_lock;
            }
            foreach (var action in _actions)
            {
                action();
            }
            lock(sync)
            {
                --_lock;
                if ((0 == _lock) && (0 < _delta.Count))
                {
                    _actions.AddRange(_delta); //Merging the delta
                    _delta.Clear();
                }
            }
        }
    }
    

    I made a few other tweaks, for the following reason:

    • reversed IF expressions to have constant value first, so if I do a
      typo and put “=” instead of “==” or “!=” etc., the compiler will
      instantly tell me of the typo.
      (: a habit I got into because my brain and fingers are often out of sync 🙂
    • preallocated _delta, and called .Clear() instead of setting it to null,
      because I find it is easier to read.
    • the various lock(_sync) {…} give you your thread safety on all instance variable access.
      🙁 with the exception of your access to _action in the enumeration itself. ):
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to create a collection class that is thread-safe by default.
I want to create a Thread safe JSP page. It is possible in Servlet
I want to create a background thread that's owned by an object. When that
I want to create a separate thread that runs its own window. Frankly, the
I want to create a worker thread that isn't the main thread so I
I want to create a thread for some db writes that should not block
I want to create a non-thread-safe chunk of code for experimentation, and those are
I want to create a class which stores DataTables, this will prevent my application
Often, when I want a class which is thread-safe, I do something like the
I am trying to create thread safe properties in C# and I want 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.