本文介绍了没有ConcurrentList< T>在.NET 4.0中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴看到新的 System.Collections.Concurrent 命名空间在.NET 4.0中,相当不错!我见过 ConcurrentDictionary ConcurrentQueue ConcurrentStack ConcurrentBag BlockingCollection

I was thrilled to see the new System.Collections.Concurrent namespace in .Net 4.0, quite nice! I've seen ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, ConcurrentBag and BlockingCollection.

有一件事情似乎神秘失踪是 ConcurrentList< T> 。我必须写我自己(或得到它的网页:))?

One thing that seems to be mysteriously missing is a ConcurrentList<T>. Do I have to write that myself (or get it off the web :) )?

我失去了一些东西明显在这里?

Am I missing something obvious here?

推荐答案

gave它一试,而回(又作:在GitHub上)。我的实现遇到了一些问题,我不会进入这里。让我告诉你,更重要的是,我学到的东西。

I gave it a try a while back (also: on GitHub). My implementation had some problems, which I won't get into here. Let me tell you, more importantly, what I learned.

首先,有没有办法,你会得到一个完整的实施中的IList&LT; T&GT; 这是无锁和线程安全的。特别是,随机插入和删除是的没有的去工作,除非你也忘了O(1)随机访问(即,除非你骗,只使用某种形式的链接列表,并让索引吸)。

Firstly, there's no way you're going to get a full implementation of IList<T> that is lockless and thread-safe. In particular, random insertions and removals are not going to work, unless you also forget about O(1) random access (i.e., unless you "cheat" and just use some sort of linked list and let the indexing suck).

我的认为的可能是值得的是线程安全的,有限的子集的IList&LT; T&GT; :尤其是一个允许一个添加,并提供随机的只读的通过索引访问(但没有插入 RemoveAt 等,还没有随机的的访问)。

What I thought might be worthwhile was a thread-safe, limited subset of IList<T>: in particular, one that would allow an Add and provide random read-only access by index (but no Insert, RemoveAt, etc., and also no random write access).

这是我的 ConcurrentList&LT; T&GT; 实施。但是,当我测试了它在多线程情况下的表现,我发现的简单地同步增加了一个名单,其中,T&GT; 更快。基本上,增加了一个名单,其中,T&GT; 闪电已经快;所涉及的计算步骤的复杂性是微乎其微的(增量指标,并分配到一个数组中的元素,这是的真的是的)。您将需要一个的吨并发写入的看到任何形式的对这个锁争用;即使这样,每次写入的平均表现仍然击败了虽然无锁实施更昂贵的 ConcurrentList&LT; T&GT;

This was the goal of my ConcurrentList<T> implementation. But when I tested its performance in multithreaded scenarios, I found that simply synchronizing adds to a List<T> was faster. Basically, adding to a List<T> is lightning fast already; the complexity of the computational steps involved is miniscule (increment an index and assign to an element in an array; that's really it). You would need a ton of concurrent writes to see any sort of lock contention on this; and even then, the average performance of each write would still beat out the more expensive albeit lockless implementation in ConcurrentList<T>.

在该列表的内部数组需要调整自身的比较少见的情况下,你支付少量费用。所以,最后我得出结论,这是的一个的利基场景,其中一个附加仅 ConcurrentList&LT; T&GT; 集合类型是有意义的:当你想保证的有关添加元素的低开销的的每一个通话的(因此,相对于摊销绩效目标)。

In the relatively rare event that the list's internal array needs to resize itself, you do pay a small cost. So ultimately I concluded that this was the one niche scenario where an add-only ConcurrentList<T> collection type would make sense: when you want guaranteed low overhead of adding an element on every single call (so, as opposed to an amortized performance goal).

这只是几乎没有有用的一类,你会觉得。

It's simply not nearly as useful a class as you would think.

这篇关于没有ConcurrentList&LT; T&GT;在.NET 4.0中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:14