c# - How to manage adding and removing specific items from collection with multiple threads? -
i have many threads can add items collection , remove specific items collection on condition. in first project, readers more writers. in second project, readers may more writers or equal or less.
- how should manage add/remove collection?
- what collection use? simple
listblocking on add/remove? - which blocking mechanism use(
lock,readerwriterlockslim,...)?
you can use data structures namespace system.collections.concurrent. encapsulate 3 aspects mentioned , can used concurrent threads without explicit locking.
see: system.collections.concurrent namespace @ msdn
for example concurrentbag<t> has icollection interface , thread-safe implementation, optimized scenarios same thread both producing , consuming data stored in bag.
if need quick object lookup might use concurrentdictionary<tkey, tvalue>.
Comments
Post a Comment