Synchronizing Two Threads w/Pthreads for Smooth Presentation

Click For Summary

Discussion Overview

The discussion centers around the synchronization of two threads using pthreads to ensure smooth presentation of images, particularly in the context of visualization and image generation. The focus is on techniques that can accommodate varying delays in the image generation thread while maintaining a consistent update rate for the visualization thread.

Discussion Character

  • Technical explanation, Debate/contested

Main Points Raised

  • One participant suggests using a double buffering technique where one thread generates an image while another thread handles rendering, allowing for efficient image swapping.
  • Another participant references older gaming practices involving double or triple buffering, where a physics thread runs at a fixed rate while the graphics thread may skip frames if it falls behind.
  • A later reply indicates that the original poster resolved their issue using triple buffering combined with a mutex, though details on the implementation are not provided.

Areas of Agreement / Disagreement

Participants present multiple approaches to the synchronization problem, including double and triple buffering, but there is no consensus on a single best method, as different techniques may be suitable depending on specific circumstances.

Contextual Notes

The discussion does not clarify the specific conditions under which each proposed method is most effective, nor does it address potential limitations or assumptions related to the implementation of these synchronization techniques.

Who May Find This Useful

Individuals interested in multithreading, graphics rendering, and synchronization techniques in programming, particularly those using pthreads.

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   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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 26 ·
Replies
26
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 0 ·
Replies
0
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K