Created
September 3, 2014 18:19
-
-
Save yemrekeskin/722e82a6509a759576bc to your computer and use it in GitHub Desktop.
FixedSizedQueue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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