简单的生产者-消费者问题,但生产者更新旧缓冲区
假设我有两个缓冲区。 生产者填充缓冲区#1,然后填充 缓冲区#2。 消费者一次消耗一个缓冲区,这是非常 慢的。 当它正在消耗缓冲区 #1 时,生产者已准备好填充 另一个缓冲区,但它们都已满,而消费者还没有 尚未完成#1。 于是,制作人就等着了。
我希望生产者更新“空闲”缓冲区,而不是等待。 也就是说,当消费者正在消费缓冲区 #1 时,生产者 一旦准备好,就应该在缓冲区 #2 上写入新数据( “旧”数据被覆盖并丢失)。 如果消费者还没有完成 但对于#1,生产者有更多数据要写入,它应该写入 再次#2,依此类推。 当消费者最终消费完#1中的所有数据时,它应该 立即开始消耗缓冲区 #2 中新写入的数据, 制作人应该继续更新#1。
(想象一下,生产者正在高速实时获取视频帧,而消费者正在慢慢地详细说明它们;消费者不介意是否跳过一些视频帧) 帧,但它必须始终处理获取的最后一帧。 这 相反,生产者不能放慢速度或等待,因为它必须获取 每帧)。
有没有办法用信号量来做这种事情? 是不是一个井—— 已知的并发问题? 并且,万一有可能延长 这个问题到n> 2 个缓冲区?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你可以只拥有一个缓冲区(队列)。 某种类型的同步队列结构来确定正在使用哪些缓冲区。 这适用于 n >= 2 个缓冲区。
我猜它会像这样工作:生产者开始写入缓冲区 1,但不会将其从队列中删除。 消费者从缓冲区 1 开始消费,并将其从队列中删除。 一旦缓冲区 1 已满,生产者就会检查队列以查看哪些缓冲区可用,结果发现只有缓冲区 2 可用。 生产者开始写入缓冲区 2。当缓冲区 2 已满时,它会再次检查队列,发现缓冲区 2 仍然可用,因此会再次写入。 一旦消费者使用完缓冲区 1,它就会从队列中删除缓冲区 2,并将缓冲区 1 放回队列中。 一旦生产者完成了 2,它将看到只有缓冲区 1 可用并开始写入。 我希望这就是你所描述的。 (我不喜欢只处理信号量,我更喜欢使用更高的数据结构,例如队列)。
要处理多个生产者,请引入另一个队列来确定生产者正在使用哪些缓冲区。 所以现在你将有一个生产者队列和一个消费者队列,我认为这可以处理你的所有情况。
Well, you could just have a buffer(queue) of buffers. Some type of synchronized queue structure to determine which buffers are being used. This would work for n >= 2 buffers.
I guess it would work like this: Producer starts writing to buffer 1, but doesn't remove it from the queue. Consumer starts consuming from buffer 1, and removes it from the queue. Once buffer 1 is full, the producer checks the queue to see which buffers are available, and it sees only buffer 2 is available. The producer starts writing to buffer 2. When buffer 2 is full, it will check the queue again and see that buffer 2 is still available, so it will write to it again. Once the consumer is done with buffer 1, it will remove buffer 2 from the queue and place buffer 1 back. Once the producer is done with 2, it will see only buffer 1 is available and start writing to it. I hope this is what you were describing. (I don't like dealing with just semaphores, I prefer to use higher data structures, eg. queues).
To deal with multiple producers, introduce another queue to determine which buffers are being used by a producer. So now you will have a producer queue and a consumer queue, and I think that handles all your situations.