I am after a collection with the following properties:
- Thread safe: It will be used in asp.net and multiple clients could try to add, remove and access members concurrently
- Max elements: I want to be able to set an upper bound, a maximum number of elements, at construction time
- TryAdd: A method that works the same as
BlockingCollection<T>.TryAdd(T)would be perfect, i.e. it would return false if the maximum number of elements has been reached - Dictionary-like: In most other respects a
ConcurrentDictionarywould be perfect, i.e. ability to identify elements by a key, remove any item (not just the first or last, which I think would be the limitation withBlockingCollection)
Before I attempt to roll my own, my questions are:
- have I missed a built in type that would put a safe ceiling on the number of elements in a collection?
- Is there a way to achieve this functionality with
BlockingCollectionsomehow?
Finally, if I do need to try and make my own, what approach should I think about? Is it as simple as a wrapped Dictionary with locks?
Example use: A chat room with a defined limit on number of participants could store the connection information of participants and reject new entrants until there is room to enter when full
The simplest solution is just make a wrapper class that uses a normal dictionary and uses a
ReaderWriterLockSlimto control thread safe access.This class implements the full
IDictionary<Tkey,TValue>interface. The way this works is all insertions pass throughTryAdd, if you are at or above the max size and try to insert a new member you get afalsefromTryAddand aInvalidOperationExceptionfrom methods that do not returnbool.The reason I did not use a
ConcurrentDictionaryis there is no good way to try to check the count before adding a new member in an atomic way, so you would need to lock anyway. You could potentially use a concurrent dictionary and remove all of myEnterReadLock's and replace theEnterWriteLock's with normallockcalls, but you would need to do performance testing to see which would do better.If you want methods like
GetOrAddit would not be hard to implement yourself.