限制生产者/消费者线程中的消费者
有一个消费者和一个生产者线程。生产者线程数据获取速度慢。它向套接字查询数据,为消费者生成数据所需的时间明显长于消费者处理和发送数据所需的时间。问题是我正在更新显示器,因此我希望更新速度减慢,以便它们看起来是连续的而不是突发更新。
我现在正在使用双缓冲区,但消费者等待缓冲区交换的时间太长,因为生产者生成数据的时间太长。也许如果我将数据分割成更小的块并使用队列代替?这样生产者就可以一次只给消费者一点点吗?有人遇到过这个问题吗?
There is a single consumer and single producer thread. The producer thread data acquisition is slow. It queries a socket for data and the time it takes to produce data for the consumer is significantly longer than the time it takes for the consumer to process and send the data out. The problem is I am updating a display so I want the updates to slow down so they appear continuous rather than update in bursts.
I am using a double buffer right now but the consumer is waiting too long for the buffers to be swapped because the producer is taking too long to produce data. Perhaps if I slice up the data into smaller blocks and use a queue instead? That way the producer would feed the consumer a little at a time? Has anyone ever run into this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不建立一个每秒更新一次屏幕的线程呢?线程可以休眠一秒钟,醒来,检查生产者和消费者正在做什么,并根据他们的进度更新屏幕。您每秒都会收到更新。如果您希望它们更快或更慢,请更改计时器间隔。
Why not have a thread that updates the screen once a sec? The thread can sleep for a sec, wake up, check into what the producer and the consumer are doing, and update the screen based on their progress. You would get updates every second. If you want them faster or slower, then change the timer interval.
我将把向客户端的发送速率锁定到基于数据请求速率的频率。我最初认为生产者会比实际快得多,所以这就是我将其制作为生产者/消费者线程的原因。这更像是一个帧速率问题,我需要以一致的速率同步输出。
I am going to lock the send rate to the client to a frequency based on the rate of the data request. I originally thought the producer was going to be much faster than it was so that is why I made it into a producer/consumer thread. This is more like a frame rate problem where I need to synchronize the output at a consistent rate.