Synchronizing Two Threads w/Pthreads for Smooth Presentation

AI Thread Summary
To synchronize two threads—one for visualization with a fixed delay and another for image generation with variable delays—using pthreads, a double buffering technique is recommended. This approach allows one thread to generate an image while another thread handles its display, effectively swapping images to ensure smooth presentation. When implemented correctly, this method minimizes the impact of the generation thread's timing on the visualization thread. Additionally, enabling vertical sync can enhance performance, as seen in older games that used double or triple buffering to manage rendering and physics threads independently. The discussion concludes with a successful implementation of triple buffering and mutexes to resolve synchronization issues.
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.
 
Technology 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 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 intervoxel
Thank you for the answers, guys. I solved the problem using triple buffering and a mutex.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top