2
\$\begingroup\$

How do you synchronise the input-handling, state-updating and rendering threads?

If a sprite position is modified due to input, the wrong position of the sprite might be drawn to the screen if the render thread draws the sprite before the update thread finishes its collision resolution routine.

How can this be done?

(The only solution I can think of would be to store input events as they are created and consume them in the update/render threads.)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I'm guessing this sort of synchronization just isn't too important... \$\endgroup\$
    – Krazy
    Sep 3, 2012 at 10:30
  • \$\begingroup\$ It would likely only produce a moment's inconsistency, but that moment would look really buggy. \$\endgroup\$ Mar 11, 2013 at 23:49

1 Answer 1

0
\$\begingroup\$

Your question is very general and I haven't faced that problem yet. But I have a basic idea how this can work. In your example, the input thread need to communicate with the state thread and the state thread need to communicate with the rendering thread.

There are two concepts coming to my mind here. First, you could make use of double buffers. For example, the state thread provides its results in the front buffer. While the renderer reads them, it continues working and stores the results in the back buffer. Each time new results are available, the buffers are switched so that the newest state is read always.

Another concept that might be favorable is a queue. For example, the input thread captures the input events and simple adds the actions to a thread safe queue. The state thread can fetch them anytime and they are removed from the queue after that.

In contrast to this way of using a double buffer, the queue ensure that all input events arrive.

My personal guess is that, as you already mentioned, the lack of synchronization wouldn't be too important. Since input, state and renderer have a higher frequency then the human eye can notice (hopefully) there might be no need for synchronization. But as I already said I haven't faced this problem yet so I cannot assure that totally.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ use double buffering being sure the 2 buffers differ at least 1 KB in memory (ptrdiff > 1024) else it will kill cache. \$\endgroup\$ Jan 31, 2015 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .