Below is a simple assembly of locks (only in the queue), which I just whipped, since you do not have access to C # 4.0. It is most likely less efficient than 4.0 concurrent collections, but it should work quite well. I didn’t reimplement all Queue methods, just in line, crossed out and looked. If you need others and cannot understand how they will be implemented, just mention this in the comments.
Once you have a working blocking collection, you can simply add to it from producer flows and delete it using consumer flows.
public class MyBlockingQueue<T>
{
private Queue<T> queue = new Queue<T>();
private AutoResetEvent signal = new AutoResetEvent(false);
private object padLock = new object();
public void Enqueue(T item)
{
lock (padLock)
{
queue.Enqueue(item);
signal.Set();
}
}
public T Peek()
{
lock (padLock)
{
while (queue.Count < 1)
{
signal.WaitOne();
}
return queue.Peek();
}
}
public T Dequeue()
{
lock (padLock)
{
while (queue.Count < 1)
{
signal.WaitOne();
}
return queue.Dequeue();
}
}
}
Servy source
share