Hi everybody,
Let's say I have two buffers. Producer fills buffer #1, then fills buffer #2. The consumer consumes one buffer a time, and it's very slow. While it is consuming buffer #1, the producer is ready to fill another buffer, but they are all full, and the consumer hasn't finished yet with #1. So, the producer waits.
Instead of waiting, I want the producer to update the "free" buffer. That is, while the consumer is consuming buffer #1, the producer should write new data on buffer #2 as soon as it has it ready (the "old" data is overwritten and lost). If the consumer hasn't finished yet with #1, and the producer has more data to write, it should write again on #2, and so on. When the consumer finally consumes all the data in #1, it should immediately start to consume the freshly-written data in buffer #2, and the producer should keep on updating #1.
(imagine the producer is acquiring video frames in realtime at high speed, while the consumer is slowly elaborating them; the consumer doesn't mind if it skips some frame, but it must always process the last frame acquired. The producer, instead, cannot slow down nor wait, because it must acquire every frame).
Is there a way to do this kind of thing with semaphores? Is it a well- known concurrency problem? And, in case, is it possible to extend this problem to n > 2 buffers?
Thanks!