Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a threadsafe and generic IList<T> in c#?

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

My question is similar to Threadsafe and generic arraylist? but I'm only looking for safety to cover adding to this list threaded, not removal or reading from it.

like image 785
Maslow Avatar asked Jul 15 '10 20:07

Maslow


2 Answers

System.Collections.Concurrent.BlockingCollection<T>

Link.

like image 192
Mau Avatar answered Oct 04 '22 03:10

Mau


.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.

like image 41
LBushkin Avatar answered Oct 04 '22 04:10

LBushkin