... continued
This lesson explains implementing a bounded buffer using Semaphores.
We'll cover the following...
Semaphore Implementation
We can also implement the bounded buffer problem using a semaphore. Since Ruby's core library doesn't have a semaphore, we'll use the semaphore implementation from the previous lesson. Let's revisit our CountingSemaphore's constructor. It takes in the maximum number of permits. We can use two semaphores, one semConsumer
and the other semProducer
. The trick is to initialize semProducer
semaphore with the maximum queue capacity of N. This allows producer threads to enqueue N items in the queue. However, the semProducere
is only released/incremented by a consumer thread whenever it consumes an item. If there are no ...