ThreadSafe的FIFO列表

ThreadSafe的FIFO列表

本文介绍了ThreadSafe的FIFO列表,具有自动大小限制管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出数据类型使用...
基本上我想要一个FIFO队列是线程安全的,并会自动扔掉旧足够的项目一旦进入到预先指定的限制

I'm trying to figure out what data type to use...Basically I want a FIFO queue that is thread-safe and will automatically throw out old enough items once it gets to a pre-specified limit.

嗯,事实上,也许更多的列表,因为我不想推到队列中,并弹出一个项目从队列的整个概念在这一点它不再可用。

Well, actually, maybe more of a list, because I don't want the whole concept of pushing onto the queue and popping an item off the queue at which point it's no longer available.

使用情况基本上是一个播放列表,我将有多达5即将到来的项目,当前播放的项目,然后约20个项目有已经发挥。因此,为什么我想这不可能是一个队列,我将访问在中间当前项目中的项目之一。我宁可不要手动管理扔掉旧项目时,列表获取到大......很明显,我可以写这一切我自己,但我不希望推倒重来如果已经存在C#。

The use case is basically for a playlist where I would have up to 5 upcoming items, the currently playing item, and then about 20 items that have already played. Hence, why I guess it cannot be a queue, I would be accessing one of the items in the middle as the "current" item. And I would rather not have to manually manage throwing away old items when the list gets to big... obviously I could write this all myself, but I don't want to reinvent the wheel if this already exists for C#.

什么,我可以用任何想法?

Any ideas of what I could use?

推荐答案

在框架那里几乎有你想要的功能的东西 - 的的。它是线程安全的队列中的大部分操作正在实施无锁的,所以它是非常快的。

In the Framework there is something almost having the functionality you want - the ConcurrentQueue . It is thread-safe queue with most operations being implemented lock-free, so it is very fast.

的唯一功能是没有的是限制,自动用完即弃...

The only function is does not have is the "limit" with automatic "throw-away"...

但是,这可以很容易地添加 - 只要创建您自己的包含私有类 ConcurrentQueue 和实施由出列的市民排队法被扔掉的部分/扔掉,直到你的极限是入队列的新元素之前得到满足。

But that can be easily added - just create you own class containing a private ConcurrentQueue and implement the "throw-away part" in your public enqueue method by Dequeuing/throwing away till your limit is satisfied before Enqueuing the new element.

编辑 - 根据注释

一个选择是使第二个排队的的ObservableCollection - 虽然不是天生线程安全(小心),这将在很容易绑定WPF ...

EDIT - as per comment:
One option would be to make the second "Queue" an ObservableCollection - though not inherently thread-safe (beware) this would be easily bind in WPF...

另一个办法是让你的类实现的接口(其中包括的IList< T> ;,的ICollection< T> ;,的IEnumerable< T> ;,的IList,ICollection的,IEnumerable的,INotifyCollectionChanged,INotifyPropertyChanged的)据此 - 这听起来很多,但其中大部分你得到容易被转发到内部实施 ConcurrentQueue 所以没有太多真正的代码编写...

见的

Another would be to make your class implement the ObservableCollection interface (which consists of IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged) accordingly - that sounds alot, but most of these you get easily implemented by relaying to the internal ConcurrentQueue so there is not much real code to write...
See http://msdn.microsoft.com/en-us/library/ms752347.aspx

这篇关于ThreadSafe的FIFO列表,具有自动大小限制管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:30