Why isn't there a ConcurrentList<T>
Solution 1
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.
Solution 2
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.
Mike
Updated on June 08, 2022Comments
-
Mike almost 2 years
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.
-
stuartd almost 8 yearsPossible duplicate of No ConcurrentList<T> in .Net 4.0?
-
-
supercat about 11 yearsThere are a variety of useful subsets of
IList<T>
which could be implemented in thread-safe fashion. For example, one could have a collection which behaves mostly like an array but also includes anAdd
method which returns the index of the newly added item. Arrays can't change size, so such a collection could fulfill roles that an array cannot. -
supercat about 11 yearsIf
T
were constrained toclass
, could one useCompareExchange
to change the last thing in the list from null to the new item and then useCompareExchange
to update_count
? If the firstCompareExchange
fails, retry the operation using the larger of_count
and (one plus the previous_count
). If the second fails, retry if_count
is less than the new value. -
Panagiotis Kanavos about 9 yearsConcurrentBag is not appropriate for general use. It's a very specialized class, used to provide quick thread-local storage and access.
-
Wolfzoon over 2 years@supercat there is no such thing as necro, so... Your scenario seems like it could just use
ConcurrentQueue
/Stack
, if all you want is to safely add to the end of an 'array'. You can still enumerate them without popping, and they stay in the same order. -
supercat over 2 years@Wolfzoon: I don't think those types support access by index, do they? If the
Add
method of an IList-like object returned the index of the new item, then multiple threads could simultaneously and independently, without conflicts, create new entries in the list and be able to update them continuously without interference, provided that any particular item would only be updated by the thread that had added it. -
supercat over 2 years@Wolfzoon: I suppose one could use a
ConcurrentDictionary<int,Whatever>
along with a staticint
that is used as a "ticket dispenser" withInterlocked.Add
to generate keys. Probably not as efficient as a more list-like design could be, but it exists as a pre-fabricated type.