线程安全集合,没有秩序,没有重复 [英] Thread-Safe collection with no order and no duplicates
问题描述
我需要持有项目不重复的一个线程安全的集合。 ConcurrentBag< T>
允许非独特的项目和的HashSet< T>
不是线程安全的。有没有这样一个集合的.NET Framework 4.5?
I need a thread-safe collection to hold items without duplicates. ConcurrentBag<T>
allows non-unique items and HashSet<T>
is not thread-safe. Is there a collection like this in .NET Framework 4.5?
推荐答案
我建议你使用一个 ConcurrentDictionary
,只是使用每个条目的虚拟价值。这很烦人的效率(让所有的虚值)的条件,但我怀疑在大多数应用中,这将是微不足道的。
I suggest you use a ConcurrentDictionary
and just use a dummy value for each entry. It's annoying in terms of efficiency (having all those dummy values) but I suspect in the majority of applications that would be insignificant.
您可能想包在您自己的 ConcurrentSet
的实现,它只是做够你的目的,这样你就不需要看抽象泄漏的你很多code。
You may well want to wrap this in your own ConcurrentSet
implementation, which just does enough for your purposes, so that you don't need to see the abstraction leakiness in much of your code.
这篇关于线程安全集合,没有秩序,没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!