The original C code was made by remis-thoughts and the full explanation is in his blog (http://remis-thoughts.blogspot.pt/2012/01/triple-buffering-as-concurrency_30.html). I encourage anyone interested in reading it for a further understanding of the original implementation.
My implementation can be found here (https://github.com/p4checo/triplebuffer-sync).
The basic idea is to have an array with 3 positions (buffers) and an atomic flag that is compare-and-swapped to define which array elements correspond to what state, at any given time. This way, only one atomic variable is used to model all 3 indexes of the array and the logic behind the triple buffering. The buffer's 3 positions are named Dirty, Clean and Snap. The producer always writes to the Dirty index, and can flip the writer to swap the Dirty with the current Clean index. The writer can request a new Snap, which swaps the current Snap index with the Clean index to get the most recent buffer. The consumer always reads the buffer in the Snap position.
I am using Release-Consume memory orderings for the Store/Load respectively, and would like to get some feedback from the community too see if I am doing it correctly for this specific problem.
For more detail please visit (http://stackoverflow.com/questions/15204578/c11-atomic-memory-ordering-is-this-a-correct-usage-of-relaxed-release-consu).
Thanks,
André