views:

97

answers:

2

Is List<T> or HashSet<T> or anything else built in threadsafe for addition only?

My question is similar to http://stackoverflow.com/questions/1278010/threadsafe-and-generic-arraylist-in-c but I'm only looking for safety to cover adding to this list threaded, not removal or reading from it.

+7  A: 
System.Collections.Concurrent.BlockingCollection<T>

Link.

Mau
+5  A: 

.NET 4.0 you could use the BlockingCollection<T>, but that is still designed to be thread safe for all operations, not just addition.

In general, it's uncommon to design a data structure that guarantees certain operations to be safe for concurrency and other to not be so. If you're concerned that there is an overhead when accessing a collection for reading, you should do some benchmarking before you go out of your way to look for specialized collections to deal with that.

LBushkin
+1 Much more info on the blocking collection, and other concerns were addressed. @LBushkin - No I was thinking there was no type built in that was fully thread-safe, it seems 4.0 is newer than the question I used to form my question.
Maslow
@Maslow: Yes, .NET 4 introduced a number of new collection types in the System.Collection.Concurrent namespace (http://msdn.microsoft.com/en-us/library/dd287108.aspx).
LBushkin