类似.NET的ConcurrentBag< T>实施? [英] How might a class like .NET's ConcurrentBag<T> be implemented?

查看:144
本文介绍了类似.NET的ConcurrentBag< T>实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己非常感兴趣的存在 ConcurrentBag< T> 类别:

I find myself very intrigued by the existence of a ConcurrentBag<T> class in the upcoming .NET 4.0 framework:


我的问题是:这个想法如何实现?我熟悉的大多数集合基本上等于(在引擎盖下)某种形式的数组,其中顺序可能不是重要的,但是顺序(这是为什么,不需要,枚举将几乎总是通过不变的集合,列表队列 Stack 等等)。

My question is: how might this idea be implemented? Most collections I'm familiar with essentially amount to (under the hood) some form of array, in which order may not "matter," but there is an order (which is why, even though it doesn't need to, enumeration will pretty much always go through an unchanged collection, be it List, Queue, Stack, etc. in the same sequence).

如果我不得不猜测,我可能建议在内部a Dictionary< T,LinkedList< T>> ;但实际上似乎很可疑,因为只使用任何类型 T 作为键是没有意义的。

If I had to guess, I might suggest that internally it could be a Dictionary<T, LinkedList<T>>; but that actually seems quite dubious considering it wouldn't make sense to use just any type T as a key.

我所期望/希望的是,这实际上是已经被解决某处的已建立的对象类型,知道这个已建立类型的人可以告诉我。这对我来说很不寻常 - 在现实生活中很容易理解的那些概念之一,但很难转化为一个可用的类作为开发人员 - 这就是为什么我很好奇的可能性。

What I'm expecting/hoping is that this is actually an established object type that has already been "figured out" somewhere, and that somebody who knows of this established type can tell me about it. It's just so unusual to me--one of those concepts that's easy to understand in real life, but is difficult to translate into a usable class as a developer--which is why I'm curious as to the possibilities.

EDIT

有些回应者建议 code>可以是内部散列表的一种形式。这是我最初的想法,但我预见了这个想法的两个问题:

Some responders have suggested that a Bag could be a form of a hashtable internally. This was my initial thought as well, but I foresaw two problems with this idea:


  1. 哈希表不是有用的,

  2. 只需在集合中跟踪对象的计数,就与存储对象不同。

如Meta-Knight建议,也许一个例子会使这个更清楚:

As Meta-Knight suggested, perhaps an example would make this more clear:

public class ExpensiveObject() {
    private ExpensiveObject() {
        // very intense operations happening in here
    }

    public ExpensiveObject CreateExpensiveObject() {
        return new ExpensiveObject();
    }
}

static void Main() {
    var expensiveObjects = new ConcurrentBag<ExpensiveObject>();

    for (int i = 0; i < 5; i++) {
        expensiveObjects.Add(ExpensiveObject.CreateExpensiveObject());
    }

    // after this point in the code, I want to believe I have 5 new
    // expensive objects in my collection

    while (expensiveObjects.Count > 0) {
        ExpensiveObject expObj = null;
        bool objectTaken = expensiveObjects.TryTake(out expObj);
        if (objectTaken) {
            // here I THINK I am queueing a particular operation to be
            // executed on 5 separate threads for 5 separate objects,
            // but if ConcurrentBag is a hashtable then I've just received
            // the object 5 times and so I am working on the same object
            // from 5 threads at the same time!
            ThreadPool.QueueUserWorkItem(DoWorkOnExpensiveObject, expObj);
        } else {
            break;
        }
    }
}

static void DoWorkOnExpensiveObject(object obj) {
    ExpensiveObject expObj = obj as ExpensiveObject;
    if (expObj != null) {
        // some work to be done
    }
}


推荐答案

如果您查看 ConcurrentBag< T> 的详细信息,发现它在内部基本上是一个自定义的链表。

If you look at the details of ConcurrentBag<T>, you'll find that it's, internally, basically a customized linked list.

由于Bags可以包含重复项,并且不能通过索引访问,所以双向链表是一个很好的选择实施。这允许锁定相当精细的插入和删除(你不必锁定整个集合,只是你插入/删除的地方的节点)。因为你不担心重复,不涉及散列。这使得一个双链表完美。

Since Bags can contain duplicates, and are not accessible by index, a doubly linked list is a very good option for implementation. This allows locking to be fairly fine grained for insert and removal (you don't have to lock the entire collection, just the nodes around where you're inserting/removing). Since you're not worried about duplicates, no hashing is involved. This makes a double linked list perfect.

这篇关于类似.NET的ConcurrentBag&lt; T&gt;实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆