Class Kata "Bounded Queue"
Develop a queue class with limited length for communication between multiple threads.
Reading threads take elements; if the queue is empty, they block and wait for the next element.
Writing threads add elements; if the queue is full, they block and wait for another thread to remove an element.
The interface of the class should look like this:
class BoundedQueue { BoundedQueue(int size) {...} void Enqueue(T element) {...} T Dequeue() {...} int Count() {...} // Number of elements in queue int Size() {...} // Max. Number of elements }
Example of use:
Neglect performance aspects.
Variation #1
Add two functions to the class:
class BoundedQueue { ... bool TryEnqueue(T element, int timeoutMsec) {...} bool TryDequeue(int timeoutMsec, out T element) {...} }
Setting/reading should optionally only block for a certain period of time. If the action is successful during this time, the true is returned, otherwise false.