Showing results for 
Search instead for 
Did you mean: 

[Synchronisation] How to implement triple buffering

Dear LabVIEWers,


In the need for displaying large images at a high performance, I want to use triple buffering in my program. This type of acquisition allows to acquire large data in buffers, and have it used without copying images back and forth between producer and consumer.


This way consumer thread doesn't wait if a buffer is ready, and producer works at max speed because it never waits or copy.
If the consumer makes the request when a buffer is ready, it is atomically turned into a "lock" state. If a buffer isn't ready, it waits for it, atomically lock it when it is ready.

The following timing diagram shows how it goes with the 3 buffers.



triple buffer.png



Traditional LabVIEW queues don't fit here because we have large buffers that we don't want to copy.


I tried to implement it with notifiers, but there is always a risk of a race condition between the producer selecting where to fill next acquisition, and locking by the consumer. With condition variables, it is easy because when a wait ends, a mutex is locked. There is no such synchronisation primitive in LabVIEW. How can I implement this?

0 Kudos
Message 1 of 2



It sounds like you need to use queues.  One queue per buffer needed.  I assume you know how large (how many elements) your images are.  You should be able to query a queue to see if it has the right amount of elements in it.  Once buffer (queue) #1 is ready you can read an image from it.  Once the read is done you can switch to buffer (queue) #2 and repeat the process.


You shouldn't have to lock a buffer while you are reading it.  If you really had the need for that then memory or number of queues should be adjusted.


With this process your producer loop should be able to run at full speed and fill the queues.  You will just have to make sure your # of buffer queues or buffer size is large enough not to have over run of them in the time needed for your image acquisition.



0 Kudos
Message 2 of 2