简体   繁体   中英

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.

For example:

  • produce_time = 10 seconds
  • consume_time = 1 second
  • number of buffers = 100

sequential processing = 100 * (10 + 1) = 1,100 seconds
double-buffered = 100 * 10 = 1,000 seconds

But, if we change the parameters so consume_time = 10 seconds:

sequential processing = 100 * (10 + 10) = 2,000 seconds
double-buffered = 100 * 10 = 1,000 seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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