25.08.2020 at 08:42 am

Triple Buffered Painters

Useful metaphor for buffering/rendering/switching canvases.

... The buffers in question are memory areas where you can store an image. When the computer creates an image, it writes it in the buffer. The display system will read from the buffer and display it on the screen several dozen times per second.

Let's say Alice is in charge of producing the images and Billy is in charge of displaying the images. The buffer is a canvas, Alice is a painter that paints things on the canvas and Billy is taking pictures of the canvas at regular intervals and shows them to you, the user.

If there's only one buffer (one canvas) it may happen that Alice is not finished with painting when Billy takes the picture. So Billy will take a picture of a partially painted canvas and show it to you. This will result in flicker: some of the images you're getting are only partially done or partially modified.

To solve this, Alice and Billy will use double-buffering: there are now two canvases, one for Alice and one for Billy. There's also a third person, Charlotte, in charge of swapping the canvases when Alice is finished. So now, Billy is always taking a picture of a complete painting. When Alice finishes her current painting, Charlotte swaps the canvases so that Billy can take a picture of the new painting, and Alice can start the next painting on the other canvas.

This works well (no more flicker) but it isn't optimized for speed: Charlotte is synchronized with Billy: she won't switch the canvases if Billy is in the process of taking a picture, she must wait for the moment Billy is showing you the picture. And then the switch itself takes some time. This means that Alice, once she's done with a painting, must wait for Charlotte to complete the switch before she can start the next painting. In the meantime Alice isn't doing anything, which isn't optimal.

A solution to optimize speed is triple buffering: our friends are now using, you guessed it, three canvases. One for Billy and two for Alice. Now, each time Alice finishes a picture, she can already start the next picture on her other canvas, she doesn't need to wait for Charlotte to switch the previous canvas with Billy. So now Billy and Charlotte are still synchronized, but Alice doesn't need to care about them, she'll always have a canvas to paint on and she doesn't waste time waiting anymore.

- Reddit

Filed under:
Words: 420 words approx.
Time to read: 1.68 mins (at 250 wpm)
, , , , , , , , ,

Other suggested posts

  1. 23.03.2023 at 11:12 am / Copywork vs Master Studies
  2. 11.06.2022 at 11:30 am / Salted Correspondences
  3. 02.01.2018 at 12:00 am / Genuinely Technical Modesty
  4. 17.12.2017 at 12:00 am / To Know C
  5. 06.10.2015 at 08:02 am / Subservient/Servile Written Malay
  6. 11.12.2013 at 12:00 am / Lighting Wizardry
  7. 03.07.2013 at 12:00 am / Craft Versus Discipline
  8. 21.07.2012 at 12:00 am / Hunted By GLaDoS
  9. 14.04.2012 at 12:00 am / Ghostly Portal
  10. 14.08.2010 at 12:00 am / 豹変
© Wan Zafran. See disclaimer.