my answer is quite simple:
is it possible to use a system_v semaphore in PHP to solve producer-consumer problem?
I mean, classical prod-cons solution can be found here.
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);
}
}
As you can see a producer increment the fillCount after putting an item in the buffer.
Anyway, as I understood in the php-doc you can only use sem_acquire and sem_release to manipulate semaphore and you cannot call sem_release without having acquired it before.
Do you have any idea of how to workaround this?
If it’s important, consider using a real message queue server: RabbitMQ, OpenAMQ, ActiveMQ, ZeroMQ, beanstalkd, etc. Using semaphores or other local mechanisms won’t scale beyond a single server.