The reason why there is no such a data structure is that all collections have lookup operation time of O(n)
. These are IndexOf
, Remove(element)
etc. They all enumerate through all elements and checking them for equality.
Only hash tables have lookup time of O(1). In concurrent scenario O(n) lookup time would lead to very long lock of a collection. Other threads will not be able to add elements during this time.
In dictionary only the cell hit by hash will be locked. Other threads can continue adding while one is checking for equality through elements in hash cell.
My advice is go on and use ConcurrentDictionary.
By the way, you are right that ConcurrentDictionary is a bit oversized for your solution. What you really need is to check quickly weather an object is in work or not. A HashSet
would be a perfect for that. It does basically nothing then Add(element)
, Contains(element)
, Remove(element)
. There is a ConcurrentHeshSet
implementation in java. For c# I found this: How to implement ConcurrentHashSet in .Net don’t know how good is it.
As a first step I would still write a wrapper with HashSet
interface around ConcurrentDictionary
bring it up and running and then try different implementations and see performance differences.