Synchronizing Two Threads w/Pthreads for Smooth Presentation

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
intervoxel
Messages
192
Reaction score
1
Two threads share a common buffer (an image, say). One thread (visualization which has an almost fixed delay). The other, image generation can have a delay either much greater or much lesser than the fixed one. What is the best paradigm to synchronize these threads using pthreads in order to achieve a smooth presentation? The visualization thread has a timer field that must be updated at a good rate, even if the main data be sparsely updated. The generation thread ideally should not be affected at all when running quickly.

Thank you for any help.
 
Physics news on Phys.org
Depending on the relative timing involved you may benefit from using double buffering [1] technique where one thread renders (generates) an image and when it is done it passes this image to another thread (usually the main GUI thread) for on-screen rendering. Then the generator starts on generating next image in a second buffer and when the GUI thread gets this is just "flips" to show this new image. If the timing is right and the synchronization done correctly the GUI thread and the render thread only need to swap two images around.

[1] https://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics
 
  • Like
Likes   Reactions: intervoxel
Following up on Filip Larsen's post, with vertical sync enabled, at least older games had the option to double or triple buffer. In the case of some games, there's a physics thread and a graphics (display) thread, and if the graphics thread gets behind, it skips the display of some of the frames. In an ideal game, the physics thread usually runs at some fixed rate, and is unaffected by the graphics thread response time. Some games operate in the ideal way, other games are impacted by graphics performance.
 
  • Like
Likes   Reactions: intervoxel
Thank you for the answers, guys. I solved the problem using triple buffering and a mutex.