... continued
This lesson explains implementing a bounded buffer using the Monitor class.
We'll cover the following...
Monitor Implementation
We can also implement the bounded buffer using the Monitor
class. In fact, using the Monitor
class saves us from busy-waiting, as a blocked thread can relinquish the monitor until it gets signaled.
Let's implement the enqueue()
method first. We'll define the entire method as a critical section as we manipulate shared data-structures within it. Below is the implementation:
Press + to interact
public void enqueue(int data){Monitor.Enter(items);while (currentSize == maxSize){Monitor.Wait(items);}if (tail == maxSize) tail = 0;items[tail] = data;currentSize++;tail++;Monitor.PulseAll(items);Monitor.Exit(items);}
The enqueue()
implementation should look eerily similar to the one which ...