I think you missed something here https://i.imgur.com/CtFLsSm.png
mtx buffer_mutex;
sem fillCount = 0;
sem emptyCount = BUFFER_SIZE;
procedure producer()
{
while (true)
{
item = produceItem();
down(emptyCount);
down(buffer_mutex);
putItemIntoBuffer(item);
up(buffer_mutex);
up(fillCount);
}
}
procedure consumer()
{
while (true)
{
down(fillCount);
down(buffer_mutex);
item = removeItemFromBuffer();
up(buffer_mutex);
up(emptyCount);
consumeItem(item);
}
}
and the same problem solved using CCRs: int avl = 0;
procedure producer()
{
while (true)
{
item = produceItem();
region R when (avl < BUFFER_SIZE)
{
putItemIntoBuffer(item);
avl++;
}
}
}
procedure consumer()
{
while (true)
{
region R when (avl > 0)
{
item = removeItemFromBuffer();
avl--;
}
consumeItem(item);
}
}
The most important thing about CCRs is that they are supposed to guarantee fairness, along with mutual exclusion. A well-designed solution uses two binary semaphore queues to achieve this.