Triple buffer implementation in Odin.
Triple buffering is a technique for safely sharing data between a single producer and a single consumer, and is a specific case of multiple buffering.
It is ideal for situations where the producer and consumer need to run at different rates but still in a time-sensitive fashion (i.e. "real time"), and where the consumer does not care about receiving every single version of data that the producer produces - it only wants whatever the latest is.
Double buffering is also a common technique but it relies on synchronising the producer/consumer in some way, for example the producer waits until the consumer has read the latest data, so could get blocked.
- Graphics rendering (fast producer) vs monitor refresh (slow consumer)
- Fast audio thread (e.g. filling a buffer with audio input from a soundcard at 100Hz+) vs GUI thread (visualising the latest buffer or an transformed version of it, such as a spectogram, every 60Hz)
- Dealing with sensor data where the consumption rate might differ from the incoming rate, such as accelerometer data.
- Install/build the Odin compiler. See the Odin Getting Started guide for more info.
- Run
maketo build the test runner inout/testrunnerormake testto build and run. N.B. if you don't have Make on your system, you can just run the commands defined in the Makefile directly.
Copy triplebuffer.odin, or the contents of it, into your project. Do what you want with it!
See the multithreaded test in tests/test_triplebuffer.odin for example usage.
Triple Buffering as a Concurrency Mechanism (II) - Remi's Thoughts