Skip to content Skip to sidebar Skip to footer

Synchronization In Android Game Loop

I'm reading about some android basic game techniques for learning purposes. I came across some code that synchronizes the access to the SurfaceHolder object used on the game loop t

Solution 1:

If you look at the reference you gave, the full quote is:

If null is not returned, this function internally holds a lock until the corresponding unlockCanvasAndPost(Canvas) call, preventing SurfaceView from creating, destroying, or modifying the surface while it is being drawn.

My emphasis - note that reading is not in the list.

So lockCanvas does not stop it being read from. I would guess the extra synchronization is intended to ensure that other threads are not reading from it at the time - which makes sense because you are calling updatePhysics which could be rather upsetting for other readers.

Solution 2:

Looking at the rest of the source code, a number of other operations outside of the SurfaceView thread are also synchronized on mSurfaceHolder. My belief therefore is that mSurfaceHolder is being used as a convenient 'lock' object to synchronize state between the thread and the rest of the application.

Post a Comment for "Synchronization In Android Game Loop"