using System.Collections.Generic;
using System.Threading;
namespace Td.Additional.Collections
{
///
/// Syncronized queue.
/// See also: http://www.mycsharp.de/wbb2/thread.php?threadid=80713.
///
/// Type of queued items.
public class SyncQueue
{
#region Private fields
private Queue _q = new Queue();
#endregion
#region Queue methods
///
/// Enqueues the specified item.
///
/// The item.
public void Enqueue(T tItem)
{
lock (this)
{
_q.Enqueue(tItem);
Monitor.Pulse(this);
System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.Name + " Enqueue ==> " + _q.Count);
}
}
///
/// Dequeues the next item. If no item in queue, method waits, until an item is in the queue.
///
///
public T Dequeue()
{
Monitor.Enter(this);
try
{
while (_q.Count == 0)
{
System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.Name + " Wait");
Monitor.Wait(this);
}
System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.Name + " Dequeue ==> " + (_q.Count - 1));
return _q.Dequeue();
}
finally
{
Monitor.Exit(this);
}
}
#endregion
}
}