I have many threads that can add items to collection and remove specific items from that collection on some condition. In first project, readers more than writers. In second project, readers may be more than writers or equal or less.
- How I should manage add/remove to that collection?
- What collection to use? Simple
Listwith blocking on add/remove? - Which blocking mechanism to use(
lock,ReaderWriterLockSlim,…)?
You can use data structures from namespace
System.Collections.Concurrent. They encapsulate all three aspects you mentioned and can be used from concurrent threads without explicit locking.See: System.Collections.Concurrent Namespace at MSDN
For example
ConcurrentBag<T>hasICollectioninterface and is a thread-safe implementation, optimized for scenarios where the same thread will be both producing and consuming data stored in the bag.If you need quick object lookup you might also use
ConcurrentDictionary<TKey, TValue>.