I need to configure the producer-consumer scheme with two threads associated with the queue (the producer starts the tasks in the queue, the consumer executes them as they arrive).
Since the line will be empty most of the time, I have to make it so that the consumer flow can sleep and be woken up as soon as the producer pushes something. However, I have to ensure that the manufacturer never blocks, not even soon. In other words, I need some one-way blocking queue.
There are free queues, but since they are, by definition, well, the lock is free, it is impossible for the consumer flow to be blocked by them.
I thought about combining a free lock queue with a condition variable. When the consumer thread discovers that the queue is empty, it will sleep, waiting for the condition to be notified. The manufacturer’s thread notifies this condition when the task is pressed into the queue, which wakes up the consumer flow (if it was sleeping). However, the condition variable must be protected by the mutex, which means that there is still little chance of blocking the producer stream when trying to get it for status notification.
I have yet to find a really good way to solve this problem, so your ideas are more welcome.
Note. I plan to use a boost stream to implement this.
Note 2: I do not consider the case when the manufacturer is trying to click something and the queue is full. It will never happen.
source share