I have two threads, one for data acquisition and the other one for display. In order to avoid from unnecessary synchronization. I use double buffering (or page flipping) as following:
- data thread is writing buffer 1 while display thread reading the buffer 2
- once writing is done for a buffer, data thread switches to the other buffer (buffer 2) and starts writing new page.
- For reading, if a buffer is in the middle of writing, display thread reads from the other buffer.
It actually works well but sometimes (1 per 100 frames) I can see tearing in the display which means there is still race condition.
So how can I implement minimal (effective) synchronization of this double buffering? A pseudo algorithm would be enough for me.