I read the wikipedia about the Producer–consumer problem https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem
It mentioned that in below code it works fine when there is only one producer and consumer.
My question is that if producer is doing up(fillCount);
and consumer is doing down(fillCount);
then the result still correct? because in my opinion it isn't atomic write operation.
semaphore fillCount = 0; // items produced
semaphore emptyCount = BUFFER_SIZE; // remaining space
procedure producer()
{
while (true)
{
item = produceItem();
down(emptyCount);
putItemIntoBuffer(item);
up(fillCount);
}
}
procedure consumer()
{
while (true)
{
down(fillCount);
item = removeItemFromBuffer();
up(emptyCount);
consumeItem(item);
}
}