After 20 years, I finally learned what ‘double buffering’ means.

In 2006, I took Java programming in high school, and we made games. The book we were learning from had us learn from experience how displayed graphics jitter and tear if you write to a buffer as it’s being displayed. The solution, double-buffering, maintains two buffers: one you write to and one that is displayed and is read-only. Once you finish writing to the first buffer, you switch which one is being displayed and which one you write to, and you write the next frame.

At the time I didn’t really understand why this worked. I blindly applied it and moved on to the fun stuff. In the next 20 years, the question barely occurred to me, and when it did I didn’t bother to learn more and understand what was going on.

Fast-forward to today, I just happen to be learning basic CUDA programming, via the book Programming Massively Parallel Processors. In this 11th lecture video, they apply double-buffering as a solution to a race condition reading and writing to the same memory buffer.

Not only did the idea finally click for me, but it made me realize why tearing and jitter happens in the graphic case: because it’s also a race condition! The refresh rate of the screen is not aligned to the time it takes to write a frame to the buffer, so the screen displays the frame part-way through the writing process.

Anyway, here’s to a life of learning new things and re-learning old things with new appreciation.


Want to respond? Send me an email, post a webmention, or find me elsewhere on the internet.

This article is syndicated on: