简体   繁体   English

C#中的并发集合

[英]Concurrent collections in C#

I'm looking for a way of getting a concurrent collection in C# or at least a collection which supports a concurrent enumerator .我正在寻找一种在C#中获取并发集合的方法,或者至少是一个支持并发枚举器的集合。 Right now I'm getting an InvalidOperationException when the collection over which I'm iterating changes.现在,当我迭代的集合发生变化时,我收到了InvalidOperationException

I could just deep copy the collection and work with a private copy but I'm wondering if there is perhaps a better way我可以深度复制收藏并使用私人副本,但我想知道是否有更好的方法

Code snippet:代码片段:

foreach (String s in (List<String>) callingForm.Invoke(callingForm.delegateGetKillStrings))
{
    //do some jazz
}

--edit-- --编辑--

I took the answer but also found that I needed to ensure that the code which was writing to the collection needed to attempt to get a lock as well.我接受了答案,但也发现我需要确保写入集合的代码也需要尝试获取锁。

private void addKillString(String s)
{
    lock (killStrings)
    {
        killStrings.Add(s);
    }
}

Other than doing a deep-copy your best bet might be to lock the collection:除了进行深度复制之外,您最好的选择可能是锁定集合:

   List<string> theList = (List<String> )callingForm.Invoke(callingForm.delegateGetKillStrings);
    lock(theList.SyncRoot) {
        foreach(string s in theList) {
               // Do some Jazz
        }
    }

So I'm not quite sure what you're asking, but the Parallel Extensions team has put together some stuff that might fit the bill.所以我不太确定你在问什么,但 Parallel Extensions 团队已经把一些可能符合要求的东西放在一起。 See this blog post in particular, about enumerating parallel collections .请特别参阅这篇关于 枚举并行集合的博客文章。 It also contains a link to download the Parallel CTP, and you can of course browse through the rest of the blog posts to get an idea of what the CTP is meant to do and how the programming model works.它还包含一个下载 Parallel CTP 的链接,您当然可以浏览其余的博客文章以了解 CTP 的用途以及编程模型的工作原理。

If you want to use the FCL collections, then locking is the only way to support iteration / modification from multiple threads that may overlap.如果您想使用 FCL 集合,那么锁定是支持来自可能重叠的多个线程的迭代/修改的唯一方法。

Be careful what you use as your lock object, though.但是,请注意您用作锁定对象的内容。 Using SyncRoot is only a good idea if the collection itself is a private member of the class that uses it.如果集合本身是使用它的类的私有成员,则使用 SyncRoot 只是一个好主意。 If the collection is protected or public, then a client of your class can take its own lock on your SyncRoot, potentially deadlocking with code in your class.如果集合是受保护的或公共的,那么您的类的客户端可以在您的 SyncRoot 上获取自己的锁,这可能会导致您的类中的代码死锁。

If you are interested in taking a look at a 3rd-party collection library, I recommend the excellent C5 Generic Collection Library .如果您有兴趣查看 3rd-party collection library,我推荐优秀的C5 Generic Collection Library They have a family of tree-based collections that can easily and safely be modified and iterated at the same time without locking - see sections 8.10 and 9.11 of their (excellent) documentation for details.他们有一系列基于树的集合,可以在不锁定的情况下轻松安全地同时修改和迭代 - 有关详细信息,请参阅他们(优秀)文档的第 8.10 和 9.11 节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM