线程安全集合,没有秩序,没有重复线程、秩序、安全

由网友(挑拨少女心)分享简介:我需要持有项目不重复的一个线程安全的集合。 ConcurrentBag< T> 允许非独特的项目和的HashSet< T> 不是线程安全的。有没有这样一个集合的.NET Framework 4.5?I need a thread-safe collection to hold items w...

我需要持有项目不重复的一个线程安全的集合。 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.

阅读全文

相关推荐

最新文章