How might a class like .NET's ConcurrentBag be implemented?

后端 未结 5 800
夕颜
夕颜 2021-02-07 21:49

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

Bags are useful for storing objects wh

5条回答
  •  梦毁少年i
    2021-02-07 22:45

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

    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.

提交回复
热议问题