Advanced Distributed Systems module at HSLU
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

32 lines
722 B

using System.Threading;
namespace Sync_Ueb04_NestedMonitor
{
class NotifyingQueue<T> {
private const int SIZE = 10;
private T[] queue = new T[SIZE];
private int head = 0;
private int tail = 0;
public void Enqueue(T item) {
lock (this) {
head++;
head %= SIZE;
queue[head] = item;
Monitor.Pulse(this);
}
}
public T Dequeue() {
lock (this) {
while (head == tail) {
Monitor.Wait(this);
}
tail++;
tail %= SIZE;
return queue[tail];
}
}
}
}