Buffer sharing

  • C/++/#
  • Thread starter intervoxel
  • Start date
  • #1
191
1

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
Filip Larsen
Gold Member
1,263
191
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
  • #3
rcgldr
Homework Helper
8,697
528
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
  • #4
191
1
Thank you for the answers, guys. I solved the problem using triple buffering and a mutex.
 

Related Threads on Buffer sharing

  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
9
Views
2K
Replies
11
Views
3K
Replies
11
Views
655
Replies
4
Views
3K
Replies
27
Views
2K
Replies
4
Views
509
  • Last Post
Replies
1
Views
648
Top