Bounded Queue

Class Kata “Bounded Queue”

Write a queue class of limited length for communication between different threads.

Reading threads can take elements from the queue – but if its empty reading is blocked until another element becomes available.

Writing threads add elements to the queue. In case the queue length has reached its limit, though, writing blocks until an element has been removed by a reading thread.

The interface of the class should look like this:

class BoundedQueue {
	BoundedQueue(int size) {...}
	void Enqueue(T element) {...}
	T Dequeue() {...}
	int Count() {...} // Current number of elements in queue
	int Size() {...} // Max. number of elements


Writing ThreadQueueReading Thread
new BoundedQueue<int>(2)
Dequeue() -> 1
Enqueue(4) // blocks
3Dequeue() -> 2
// is released3,4
4Dequeue() -> 3
Dequeue() -> 4
Dequeue() // blocks
-> 5 // is released

Performance issues can be neglected.

Variations #1

Add two functions to the class:

class BoundedQueue {
	bool TryEnqueue(T element, int timeoutMsec) {...}
	bool TryDequeue(int timeoutMsec, out T element) {...}

Reading/writing optionally should block only for a limited time. If the function succeeds before timeout, true is returned, otherwise false.