Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created September 3, 2014 18:19
Show Gist options
  • Save yemrekeskin/722e82a6509a759576bc to your computer and use it in GitHub Desktop.
Save yemrekeskin/722e82a6509a759576bc to your computer and use it in GitHub Desktop.
FixedSizedQueue
namespace Sample
{
using System.Collections.Concurrent;
public class FixedSizedQueue<T>
{
ConcurrentQueue<T> q = new ConcurrentQueue<T>();
public int Limit { get; internal set; }
public FixedSizedQueue(int limit)
{
Limit = limit;
}
public void Enqueue(T obj)
{
q.Enqueue(obj);
lock (this)
{
T overflow;
while (q.Count > Limit && q.TryDequeue(out overflow)) ;
}
}
public T[] DequeueAll()
{
var returnValue = new T[q.Count];
lock (this)
{
int i = 0;
T item;
while (q.TryDequeue(out item))
{
returnValue[i] = item;
++i;
};
}
return returnValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment