简体   繁体   English

双缓冲设计I / O同步

[英]double buffer design I/O synchronization

If I have an application that that produces data slowly but consumes it quickly, would it be a good candidate for a double buffer implementation? 如果我有一个缓慢生成数据但是快速消耗它的应用程序,它是否适合双缓冲区实现? The basic idea would be to have the producer fill the back buffer while the consumer processes the front buffer. 基本思想是让生产者填充后台缓冲区,而消费者处理前台缓冲区。 I do not want the client to appear as though it is waiting for data. 我不希望客户端看起来像是在等待数据。 I want to balance out the producing and the consuming. 我想平衡生产和消费。 How can I achieve this functionality? 我该如何实现此功能? Even if I have a back buffer thread.... it will have to be synchronized with the front buffer thread so the front buffer knows when there is new data (buffers been swapped). 即使我有一个后缓冲线程....它必须与前缓冲线程同步,因此前缓冲区知道何时有新数据(缓冲区被交换)。 If back buffer thread takes too long to produce its data then the front buffer will have to wait to process it. 如果后台缓冲线程需要太长时间才能生成数据,那么前台缓冲区必须等待处理它。

void fill_back_buffer() {

   //wait to fill buffer
   //fill back buffer


   //swap buffers and notify other thread
}

void process_data() {

  //wait to see if buffers have been swapped


  //buffers been swapped so send data out
  //while sending data out start filling back buffer with new data
}

If your application "produces data slowly but consumes it quickly", that's going to limit the gains you can get from double-buffering. 如果您的应用程序“缓慢生成数据但快速消耗它”,那么这将限制双缓冲可以获得的收益。

If it takes 10 seconds to produce a buffer full of data, and 1 second to consume it, double-buffering can increase your throughput 10%, but if producing and consuming both take the same amount of time, double-buffering may double your throughput. 如果生成一个充满数据的缓冲区需要10秒钟,而消耗它需要1秒钟,那么双缓冲可以将吞吐量提高10%,但如果生产和消耗两者都需要相同的时间,那么双缓冲可能会使吞吐量增加一倍。

For example: 例如:

  • produce_time = 10 seconds produce_time = 10秒
  • consume_time = 1 second consume_time = 1秒
  • number of buffers = 100 缓冲区数= 100

sequential processing = 100 * (10 + 1) = 1,100 seconds 顺序处理= 100 *(10 + 1)= 1,100秒
double-buffered = 100 * 10 = 1,000 seconds 双缓冲= 100 * 10 = 1,000秒

But, if we change the parameters so consume_time = 10 seconds: 但是,如果我们更改参数,那么consume_time = 10秒:

sequential processing = 100 * (10 + 10) = 2,000 seconds 顺序处理= 100 *(10 + 10)= 2,000秒
double-buffered = 100 * 10 = 1,000 seconds 双缓冲= 100 * 10 = 1,000秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM