This has nothing to do with locking. The thing about a queue is that the consumer will automatically block trying to dequeue if there is nothing in the queue, while there is nothing to stop the producer pushing more stuff. Thus, depending on the relative speeds of the producer and consumer, the queue can grow without limit.
Your sleep(3) in the consumer is (probably) pointless; what you want is for the producer to pause up once the queue reaches a certain size, eg
while ($q->pending > 100) {
sleep 1;
}
$q->enqueue(...);
Dave.