Why isn't there a ConcurrentList<T>

16k views Asked by At

The new namespace System.Collections.Concurrent contains concurrent collections for dictionary, queue and stack among other classes. Anyone know why is it that there is no ConcurrentList?

UPDATE

I've posted a new question explaining my current scenario. I preferred that to changing the whole sense of the original question. Here's the link to the new question.

3

There are 3 answers

4
CodesInChaos On BEST ANSWER

Random access doesn't make much sense on a data structure that's changed from another thread.

If you look at the concurrent collections, you'll notice that their interface is specifically designed to work well with multi threaded access. I can't think of a useful list-like interface that works well with multithreaded code.

Random multi threaded access can make sense if the elements are never moved, but then you have an array.

1
Ian Mercer On

If two threads added items simultaneously what expectation of 'order' would you have? ConcurrentBag is more appropriate if all you want is a collection of items.

1
Dan Tao On

Back in 2011 I wrote a ConcurrentList<T> class (code available on GitHub), which is thread-safe, lock-free, and implements some of the IList<T> interface.

Notably, any operations that mutate the list other than Add are not supported; i.e., it is an append-only collection. So Insert, RemoveAt, etc. don't work.