Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
1  
I'm guessing this sort of synchronization just isn't too important... –  Krazy Sep 3 '12 at 10:30
    
It would likely only produce a moment's inconsistency, but that moment would look really buggy. –  Anko Mar 11 '13 at 23:49
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.