What is wrong with my @synchronized block?

Posted by hyn on Stack Overflow See other posts from Stack Overflow or by hyn
Published on 2010-05-16T19:05:16Z Indexed on 2010/05/16 19:10 UTC
Read the original article Hit count: 202

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?

© Stack Overflow or respective owner

Related posts about nsthread

Related posts about multithreading