I have 2 threads in my application, a game update thread and render/IO/main thread. My update thread updates the game state, and the render thread renders the scene based on the updated values of the game state models and a few other variables stored inside an object (gameEngine).
The render thread gets executed while the game thread is still updating, which is a problem, so it appeared to me the solution is to use @synchronized like this:
@synchronized(gameEngine)
{
[gameEngine update];
nextUpdate = now + GAME_UPDATE_INTERVAL;
gameEngine.lastGameUpdateInterval = now - lastUpdate;
gameEngine.lastGameUpdateTime = now;
lastUpdate = now;
}
But the render thread still accesses the gameEngine object between -update
and the last 3 lines of the block. Why is this?