I/O Systems & Device Management

double buffering

Imagine two trays at a buffet line. While guests are being served from one tray, the kitchen is refilling the second tray out of sight. The moment the first empties, the staff swap them — full tray out, empty tray back to the kitchen — so service never pauses. Double buffering is exactly this with two memory buffers: one is being filled (or drained) while the other is being used, and the OS flips between them so producer and consumer can work at the same time.

Concretely, suppose a device is delivering a stream of data and a program is processing it. With a single buffer, the device must wait while the program reads the buffer, and the program must wait while the device fills it — they take turns and each idles. With two buffers, the device fills buffer A while the program processes buffer B; when both are done, they swap roles. Now the device fills B while the program chews on A. As long as both stages take roughly the same time, neither ever waits for the other, and throughput nearly doubles compared with the stop-and-go single-buffer case.

Why it matters: double buffering is everywhere that a steady stream must not stutter. Graphics is the classic case: the screen is shown from one framebuffer while the next frame is drawn into a second, then they swap during the blank interval — this is why you do not see half-drawn frames (tearing). Audio playback, video capture, and tape or disk streaming use the same trick. The honest limit: double buffering only smooths things if the two stages are well matched in speed; if one stage is persistently slower, you eventually stall anyway, and very bursty workloads may need more than two buffers (a ring of buffers).

A program reads a file in chunks. The disk fills buffer A; while the program processes A, the disk is already filling buffer B; then they swap. The disk and the CPU work simultaneously instead of waiting for each other.

Fill one buffer while using the other, then flip — overlap replaces idle waiting.

Double buffering helps only when the producer and consumer run at similar speeds; if one is consistently the bottleneck, the extra buffer cannot save you — it just delays the inevitable stall by one buffer's worth.

Also called
ping-pong buffering雙重緩衝