I have a producer and a consumer. Producer fills its internal queue with objects, consumer takes these objects one by one. I want to synchronize the cosumer with the producer, so that the consumer blocks when there are no objects ready, and I want to synchronize the producer with itself, so that it stops producing when the queue is full (and starts again when there’s space). How do I do that? I was able to solve a simpler case without the queue using NSConditionalLock, but with the queue the problem looks more complex.
I have a producer and a consumer. Producer fills its internal queue with objects,
Share
I ended up using two semaphores,
objectsReadyandbufferFreeSlots:The
bufferFreeSlotsis initialized to the maximum queue size. So far it seems to work, but God knows this is an act of faith, not a solid confidence.