188

I want an implementation of List<T> as a property which can be used thread-safely without any doubt.

Something like this:

private List<T> _list;

private List<T> MyT
{
    get { // return a copy of _list; }
    set { _list = value; }
}

It seems still I need to return a copy (cloned) of collection so if somewhere we are iterating the collection and at the same time the collection is set, then no exception is raised.

How to implement a thread-safe collection property?

7
  • 9
    use locks, that should do it.
    – atoMerz
    Commented May 3, 2011 at 19:05
  • Can use use a thread-safe implementation of IList<T> (vs List<T>)?
    – Greg
    Commented May 3, 2011 at 19:13
  • 3
    Have you checked SynchronizedCollection<T> ? Commented May 23, 2016 at 7:22
  • 1
    Use BlockingCollection or ConcurrentDictionary Commented Sep 18, 2018 at 4:28
  • What operations you need to do with object behind the property? Is it possible that you don't need everything that List<T> implements? If yes, then could you please provide an interface that you need instead of asking about everything that List<T> already have? Commented Nov 16, 2018 at 18:39

15 Answers 15

242

If you are targetting .Net 4 there are a few options in System.Collections.Concurrent Namespace

You could use ConcurrentBag<T> in this case instead of List<T>

10
  • 10
    Like List<T> and unlike Dictionary, ConcurrentBag accepts duplicates.
    – The Light
    Commented May 24, 2012 at 9:17
  • 183
    ConcurrentBag is unordered collection, so unlike List<T> it does not guarantee ordering. Also you cannot access items by index. Commented Mar 7, 2013 at 13:56
  • 18
    @RadekStromský is right, and in the case you wanna an ordered concurrent list, you could try ConcurrentQueue (FIFO) or ConcurrentStack (LIFO).
    – Caio Cunha
    Commented Dec 30, 2013 at 21:14
  • 15
    Maybe SynchronizedCollection<T> ? Commented May 23, 2016 at 7:24
  • 23
    ConcurrentBag doesn't implement IList and is not actually thread safe version of List Commented Apr 3, 2017 at 20:55
138

Even as it got the most votes, one usually can't take System.Collections.Concurrent.ConcurrentBag<T> as a thread-safe replacement for System.Collections.Generic.List<T> as it is (Radek Stromský already pointed it out) not ordered.

But there is a class called System.Collections.Generic.SynchronizedCollection<T> that is already since .NET 3.0 part of the framework, but it is that well hidden in a location where one does not expect it that it is little known and probably you have never ever stumbled over it (at least I never did).

SynchronizedCollection<T> is compiled into assembly System.ServiceModel.dll (which is part of the client profile but not of the portable class library).

Edit: There is also a NuGet package "System.ServiceModel.Primitives" that since version ~4.6.0 contains the System.Collections.Generic.SynchronizedCollection<T>.
(Credits to Roi Shabtai)

5
  • 2
    Additional helpful discussion of this option: stackoverflow.com/a/4655236/12484 Commented May 19, 2016 at 15:54
  • 7
    @denfromufa it look likes they added this in .net core 2.0 learn.microsoft.com/en-gb/dotnet/api/…
    – Cirelli94
    Commented Mar 7, 2018 at 15:00
  • 7
    ConcurrentBag is not a replacement of list. It doesn't behaves like list, You cannot remove elements like lists. In lists you can specify the item to be removed, you cannot do this with concurrent bags Commented Oct 28, 2021 at 8:21
  • Unfortunately this isn't completely thread safe. Enumarations are not thread safe and that's one of the main reasons why one would pick this over other types.
    – Ivan Ičin
    Commented Mar 16, 2022 at 13:46
  • I just broke my website using concurrentbag as a replacement to list. I thought it may have failed but I wasn’t sure. Would’ve been a lot easier if there was just a concurrent list like thing. I think I’ll try the stack next :) Commented Jun 23, 2022 at 18:05
22

C#'s ArrayList class has a Synchronized method.

var threadSafeArrayList = ArrayList.Synchronized(new ArrayList());

This returns a thread safe wrapper around any instance of IList. All operations need to be performed through the wrapper to ensure thread safety.

Note, however, that this works by locking the collection, which can have performance impact. From the MS docs:

The wrapper works by locking the entire collection on every add or remove operation. Therefore, each thread that's attempting to access the collection must wait for its turn to take the one lock. This process isn't scalable and can cause significant performance degradation for large collections. Also, the design isn't protected from race conditions.

This may not be a deal breaker for you– but it's worth keeping in mind.

4
  • 1
    What language are you talking about? Commented Dec 22, 2017 at 18:39
  • Java? One of the few features I miss about it. But it's usually written as: Collections.synchronizedList(new ArrayList());
    – Nick
    Commented May 29, 2018 at 21:21
  • 3
    This is valid C# assuming you have a using System.Collections or you could use var System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList()); Commented Oct 1, 2018 at 16:51
  • It's not generic :( Commented Nov 6, 2022 at 8:28
21

I would think making a sample ThreadSafeList class would be easy:

public class ThreadSafeList<T> : IList<T>
{
    protected List<T> _internalList = new List<T>();

    // Other Elements of IList implementation

    public IEnumerator<T> GetEnumerator()
    {
        return Clone().GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return Clone().GetEnumerator();
    }

    protected static object _lock = new object();

    public List<T> Clone()
    {
        List<T> newList = new List<T>();

        lock (_lock)
        {
            _internalList.ForEach(x => newList.Add(x));
        }

        return newList;
    }
}

You simply clone the list before requesting an enumerator, and thus any enumeration is working off a copy that can't be modified while running.

13
  • 4
    Correct, it is a shallow copy. The point was to simply have a cloned set that would be safe to iterate over (so newList does not have any items added or removed which would invalidate the enumerator).
    – Tejs
    Commented Jan 3, 2013 at 18:47
  • 13
    Should the _lock be static?
    – Mike Ward
    Commented Nov 18, 2013 at 16:43
  • 5
    Another thought. Is this implementation threadsafe for multiple writers? If not, maybe it should be called a ReadSafeList.
    – Mike Ward
    Commented Nov 18, 2013 at 16:49
  • 11
    @MikeWard - I don't think it should be, all instance will lock when any instance is being cloned!
    – Josh M.
    Commented Jan 16, 2014 at 13:09
  • 2
    Why "reinvent the square wheel" when MS has now made many different types of Concurrent alternatives.
    – bytedev
    Commented Sep 21, 2018 at 14:31
19

Even accepted answer is ConcurrentBag, I don't think it's real replacement of list in all cases, as Radek's comment to the answer says: "ConcurrentBag is unordered collection, so unlike List it does not guarantee ordering. Also you cannot access items by index".

So if you use .NET 4.0 or higher, a workaround could be to use ConcurrentDictionary with integer TKey as array index and TValue as array value. This is recommended way of replacing list in Pluralsight's C# Concurrent Collections course. ConcurrentDictionary solves both problems mentioned above: index accessing and ordering (we can not rely on ordering as it's hash table under the hood, but current .NET implementation saves order of elements adding).

5
  • I didn't down-vote and there is no reason for it IMO. You are right but the concept is already mentioned in some answers. To me, the point was there is an new thread-safe collection in .NET 4.0 which I was not aware of. Not sure used Bag or Collection for the situation. +1
    – Xaqron
    Commented Mar 31, 2017 at 23:18
  • 4
    This answer has several problems: 1) ConcurrentDictionary is a dictionary, not a list. 2) It is not guaranteed to preserver order, as your own answer states, which contradicts your stated reason for posting an answer. 3) It links to a video without bringing the relevant quotes into this answer (which might not be in concordance with their licensing anyway).
    – jpmc26
    Commented May 18, 2018 at 17:47
  • 1
    You can't rely on things like current implementation if it is not explicitly guaranteed by the documentation. Implementation may change any time without a notice. Commented Nov 14, 2018 at 18:55
  • 1
    @jpmc26, yes it is not a full replacement for List of course, however the same is with ConcurrentBag as an accepted answer - it is not strict replacement of List but work around. To reply your concerns: 1) ConcurrentDictionary is a dictionary not a list you are right, however list has array behind, which can index in O(1) same as dictionary with int as a key 2) yes order is not guaranteed by doc (even though it is preserved), but accepted ConcurrentBag cannot guarantee order as well in multithreaded scenarios
    – tytyryty
    Commented Oct 21, 2019 at 8:53
  • 1
    That suggestion has potential in my opinion. If Dictionary.Count is used as a key (in case there are no deletes), any thread may add values like this while (!myDict.TryAdd(myDict.Count, myValue)) { } (or use an atomic increment of a counter in case there might be deletes). This would guarantee the values can be brought into the original order when retrieving them.
    – Christoph
    Commented Dec 11, 2019 at 16:48
12

In .NET Core (any version), you can use ImmutableList, which has all the functionality of List<T>.

3
  • Yeah, but you still need to wrap it into locks - it's immutable, so when you modify it you need to re-assign the new list, which is not thread-safe. Commented Nov 6, 2022 at 8:25
  • 2
    @MrPatience Nope! The concurrency problems of a List is that a thread modifies it while another thread is iterating it. In this case if a thread is iterating the list, and another thread modifies it, the iterated list is unchanged, and a new modified list is created, so there is no concurrency problem. Remember that the objects are passed by reference, so the iterating thread still has the reference to the original, unchanged list. If you don't believe it, just check the docs: learn.microsoft.com/en-us/dotnet/api/…
    – JotaBe
    Commented Nov 7, 2022 at 12:22
  • While this is true for ImmutableList it's not always true, it does not take necessarily writing while reading to void thread-safety. As I read somewhere here, iteration can happen in many ways, even by altering some variables of the collection. So if it is not explicitly marked as thread safe, it must not be considered such. Even if you are just reading concurrently.
    – Xriuk
    Commented Mar 26 at 19:34
11

In reply to all of the answers since my original one:

People found these solutions:

  • use SynchronizedCollection - the source code is almost identical to the SynchronizedList below, with the same features and issues
  • use ArrayList.Synchronized - this will return a SyncIList, which is the same thing as the SynchronizedList below, only not generic
  • using a thread safe form of a list, where on every access you clone it - I believe the implementation presented could be improved significantly using ThreadLocal or AsyncLocal, but it would still fail any performance tests
  • use various combinations of classes in the Collections.Concurrent namespace - these contains some good options for ICollection, but NOT for IList for index access
  • use a ConcurrentDictionary<int, T>, with indexes as keys, to emulate an IList - this is one of the best ideas I've seen, but it's not really in the spirit of an IList, which implies O(1) index read/append complexity and some complexity in insert/deletes, as well as an O(n) space complexity. Also, what about IndexOf and sorting operations?

Most complaints against the SynchronizedList class were related to:

  • slowness of the lock mechanism - the performance calculation varies wildly based on the scenario where the list is used, so this is a valid option for a vague requirement
  • using lock(object) and not using SemaphoreSlim - OK, this is my complaint :) but fixing the code to use it is trivial

A more complex system of locks can be implemented, like for individual rows, groups of rows, etc. That will start to look like implementing your own database, though. Writing a high performance collection is an art and is very tightly coupled to the specific scenario you want to use it in.

I still believe that for a general use scenario the simple solution here is the most versatile.

The C5 collection library, which is one of the inspirations for great collection design, handles concurrency thus:

  • no concurrent collections (by design) because they feel a simple lock mechanism can be implemented, but scenarios get very complex when multiple collections are getting accessed at the same time
  • "A tree-based collection can be safely enumerated while modifying it" - where they recommend "pattern 66": "one can take a snapshot of the tree and then enumerate the items of that snapshot, while modifying the original tree at the same time"

Original answer:

If you look at the source code for List of T (https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,c66df6f36c131877) you will notice there is a class there (which is of course internal - why, Microsoft, why?!?!) called SynchronizedList of T. I am copy pasting the code here:

   [Serializable()]
    internal class SynchronizedList : IList<T> {
        private List<T> _list;
        private Object _root;

        internal SynchronizedList(List<T> list) {
            _list = list;
            _root = ((System.Collections.ICollection)list).SyncRoot;
        }

        public int Count {
            get {
                lock (_root) { 
                    return _list.Count; 
                }
            }
        }

        public bool IsReadOnly {
            get {
                return ((ICollection<T>)_list).IsReadOnly;
            }
        }

        public void Add(T item) {
            lock (_root) { 
                _list.Add(item); 
            }
        }

        public void Clear() {
            lock (_root) { 
                _list.Clear(); 
            }
        }

        public bool Contains(T item) {
            lock (_root) { 
                return _list.Contains(item);
            }
        }

        public void CopyTo(T[] array, int arrayIndex) {
            lock (_root) { 
                _list.CopyTo(array, arrayIndex);
            }
        }

        public bool Remove(T item) {
            lock (_root) { 
                return _list.Remove(item);
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            lock (_root) { 
                return _list.GetEnumerator();
            }
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator() {
            lock (_root) { 
                return ((IEnumerable<T>)_list).GetEnumerator();
            }
        }

        public T this[int index] {
            get {
                lock(_root) {
                    return _list[index];
                }
            }
            set {
                lock(_root) {
                    _list[index] = value;
                }
            }
        }

        public int IndexOf(T item) {
            lock (_root) {
                return _list.IndexOf(item);
            }
        }

        public void Insert(int index, T item) {
            lock (_root) {
                _list.Insert(index, item);
            }
        }

        public void RemoveAt(int index) {
            lock (_root) {
                _list.RemoveAt(index);
            }
        }
    }

Personally I think they knew a better implementation using SemaphoreSlim could be created, but didn't get to it.

8
  • 3
    +1 Locking the whole collection (_root) in each access (read/write) makes this a slow solution. Maybe it is better for this class to remain internal.
    – Xaqron
    Commented Aug 12, 2018 at 20:38
  • 5
    This implementation is not thread-safe. It still throws "System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'" Commented Nov 28, 2018 at 19:17
  • 5
    That is not related to thread safety, but to the fact that you are iterating and changing the collection. The exception is thrown by the enumerator when it sees the list was changed. To get around this you need to implement your own IEnumerator or change the code so that it doesn't iterate and change the same collection at the same time. Commented Nov 30, 2018 at 11:41
  • 3
    It's not thread-safe because the collection can be changed during the "synchronized" methods. That absolutely is part of thread safety. Consider one thread calls Clear() after another calls this[index] but before the lock is activated. index is no longer safe to use and will throw an exception when it finally executes.
    – Suncat2000
    Commented Jan 30, 2020 at 16:57
  • 2
    Well, you could as easily cause the same issue in single thread by having a reference to the list, then calling another function that clears the list and then trying to access an element directly by index. index-based access should be guarding against those exceptions IMHO, but I do see your point of use-friendliness. Commented Apr 29, 2023 at 4:40
3

I would suggest anyone dealing with a List<T> in multi-threading scenarios to take look at Immutable Collections in particular the ImmutableArray.

I've found it very useful when you have:

  1. Relatively few items in the list
  2. Not so many read/write operations
  3. A LOT of concurrent access (i.e. many threads that access the list in reading mode)

Also can be useful when you need to implement some sort of transaction-like behavior (i.e. revert an insert/update/delete operation in case of fail)

3

It seems like many of the people finding this are wanting a thread safe indexed dynamically sized collection. The closest and easiest thing I know of would be.

System.Collections.Concurrent.ConcurrentDictionary<int, YourDataType>

This would require you to ensure your key is properly incremented if you want normal indexing behavior. If you are careful .count() could suffice as the key for any new key value pairs you add.

2
  • 9
    Why should key be incriminated when it wasn't the key's fault?
    – Suncat2000
    Commented Jan 30, 2020 at 16:59
  • @Suncat2000 ha!
    – Richard II
    Commented Jul 29, 2020 at 15:25
2

You can also use the more primitive

Monitor.Enter(lock);
Monitor.Exit(lock);

which lock uses (see this post C# Locking an object that is reassigned in lock block).

If you are expecting exceptions in the code this is not safe but it allows you to do something like the following:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;

public class Something
{
    private readonly object _lock;
    private readonly List<string> _contents;

    public Something()
    {
        _lock = new object();

        _contents = new List<string>();
    }

    public Modifier StartModifying()
    {
        return new Modifier(this);
    }

    public class Modifier : IDisposable
    {
        private readonly Something _thing;

        public Modifier(Something thing)
        {
            _thing = thing;

            Monitor.Enter(Lock);
        }

        public void OneOfLotsOfDifferentOperations(string input)
        {
            DoSomethingWith(input);
        }

        private void DoSomethingWith(string input)
        {
            Contents.Add(input);
        }

        private List<string> Contents
        {
            get { return _thing._contents; }
        }

        private object Lock
        {
            get { return _thing._lock; }
        }

        public void Dispose()
        {
            Monitor.Exit(Lock);
        }
    }
}

public class Caller
{
    public void Use(Something thing)
    {
        using (var modifier = thing.StartModifying())
        {
            modifier.OneOfLotsOfDifferentOperations("A");
            modifier.OneOfLotsOfDifferentOperations("B");

            modifier.OneOfLotsOfDifferentOperations("A");
            modifier.OneOfLotsOfDifferentOperations("A");
            modifier.OneOfLotsOfDifferentOperations("A");
        }
    }
}

One of the nice things about this is you'll get the lock for the duration of the series of operations (rather than locking in each operation). Which means that the output should come out in the right chunks (my usage of this was getting some output onto screen from an external process)

I do really like the simplicity + transparency of the ThreadSafeList + that does the important bit in stopping crashes

0

I believe _list.ToList() will make you a copy. You can also query it if you need to such as :

_list.Select("query here").ToList(); 

Anyways, msdn says this is indeed a copy and not simply a reference. Oh, and yes, you will need to lock in the set method as the others have pointed out.

-1

Looking at the original sample one may guess that the intention was to be able to simply replace the list with the new one. The setter on the property tells us about it.

The Micrisoft's Thread-Safe Collections are for safely adding and removing items from collection. But if in the application logic you are intending to replace the collection with the new one, one may guess, again, that the adding and deleting functionality of the List is not required.

If this is the case then, the simple answer would be to use IReadOnlyList interface:

 private IReadOnlyList<T> _readOnlyList = new List<T>();

    private IReadOnlyList<T> MyT
    {
       get { return _readOnlyList; }
       set { _readOnlyList = value; }
    }

One doesn't need to use any locking in this situation because there is no way to modify the collection. If in the setter the "_readOnlyList = value;" will be replaced by something more complicated then the lock could be required.

-3

Basically if you want to enumerate safely, you need to use lock.

Please refer to MSDN on this. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Here is part of MSDN that you might be interested:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A List can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

1
  • 2
    Not true at all. You can use Concurrent sets.
    – ANeves
    Commented Feb 5, 2015 at 16:12
-3

Here is the class for thread safe list without lock

 public class ConcurrentList   
    {
        private long _i = 1;
        private ConcurrentDictionary<long, T> dict = new ConcurrentDictionary<long, T>();  
        public int Count()
        {
            return dict.Count;
        }
         public List<T> ToList()
         {
            return dict.Values.ToList();
         }

        public T this[int i]
        {
            get
            {
                long ii = dict.Keys.ToArray()[i];
                return dict[ii];
            }
        }
        public void Remove(T item)
        {
            T ov;
            var dicItem = dict.Where(c => c.Value.Equals(item)).FirstOrDefault();
            if (dicItem.Key > 0)
            {
                dict.TryRemove(dicItem.Key, out ov);
            }
            this.CheckReset();
        }
        public void RemoveAt(int i)
        {
            long v = dict.Keys.ToArray()[i];
            T ov;
            dict.TryRemove(v, out ov);
            this.CheckReset();
        }
        public void Add(T item)
        {
            dict.TryAdd(_i, item);
            _i++;
        }
        public IEnumerable<T> Where(Func<T, bool> p)
        {
            return dict.Values.Where(p);
        }
        public T FirstOrDefault(Func<T, bool> p)
        {
            return dict.Values.Where(p).FirstOrDefault();
        }
        public bool Any(Func<T, bool> p)
        {
            return dict.Values.Where(p).Count() > 0 ? true : false;
        }
        public void Clear()
        {
            dict.Clear();
        }
        private void CheckReset()
        {
            if (dict.Count == 0)
            {
                this.Reset();
            }
        }
        private void Reset()
        {
            _i = 1;
        }
    }
1
  • 1
    _i++ is not threadsafe. you have to use an atomic add when you increment it and probably mark it volatile too. CheckReset() is not threadsafe. Anything can happen in between the conditional check and call to Reset(). Don't write your own multithreading utilities.
    – user6713871
    Commented Aug 7, 2020 at 18:40
-17

Use the lock statement to do this. (Read here for more information.)

private List<T> _list;

private List<T> MyT
{
    get { return _list; }
    set
    {
        //Lock so only one thread can change the value at any given time.
        lock (_list)
        {
            _list = value;
        }
    }
}

FYI this probably isn't exactly what your asking - you likely want to lock farther out in your code but I can't assume that. Have a look at the lock keyword and tailor its use to your specific situation.

If you need to, you could lock in both the get and set block using the _list variable which would make it so a read/write can not occur at the same time.

7
  • 2
    That's not going to solve his problem; it only stops threads from setting the reference, not adding to the list.
    – Tejs
    Commented May 3, 2011 at 19:07
  • And what if one thread is setting the value while another is iterating the collection (it's possible with your code).
    – Xaqron
    Commented May 3, 2011 at 19:07
  • Like I said, the lock will probably have to be moved out further in the code. This is just an example of how to use the lock statement.
    – Josh M.
    Commented May 3, 2011 at 21:02
  • 2
    @Joel Mueller: Sure, if you manufacturer some silly example like that. I'm just trying to illustrate that the asker should look into the lock statement. Using a similar example I could argue that we shouldn't use for loops since you could deadlock the application with hardly any effort: for (int x = 0; x >=0; x += 0) { /* Infinite loop, oops! */ }
    – Josh M.
    Commented May 4, 2011 at 11:39
  • 6
    I never claimed your code meant instant deadlock. It's a bad answer to this particular question for the following reasons: 1) It doesn't protect against the contents of the list being modified during enumeration of the list, or by two threads at once. 2) Locking the setter but not the getter means the property is not really thread-safe. 3) Locking on any reference that is accessible from outside the class is widely considered a bad practice, as it dramatically increases the chances of accidental deadlock. That's why lock (this) and lock (typeof(this)) are big no-no's. Commented May 4, 2011 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.