Android MultiThread game
===

I am working on a simple android game. I came to a point that I would like to optimize my engine with multithreading. I am working in OpenGL ES, Android 2.2

Now UpdateGame() and RenderScene() run in single thread and are executed in onDrawFrame(GL10 gl)

I have a RenderObject class that has Position, Rotation, Scale, and Color members. All RenderObjects are created when I start my game and are stored in RenderObject array.

In UpdateGame() function I go through RenderObjects in RenderObject array and update new Position, Rotation, Scale and Color.

In RenderScene() function I go through RenderObjects in RenderObject array and render them with new Position, Rotation, Scale and Color.

This works ok in single thread.

So then I tried create a thread for UpdateGame(). 

So my idea is to:

Update Thread:................Render Thread:

Update Frame 0

Update Frame 1................Render Frame 0....Update and Render work in parallel

Update Frame 2................Render Frame 1

Update Frame 3................Render Frame 2


But first I modified RenderObject class so that it has RenderPosition, RenderRotation, RenderScale and RenderColor members. 

These members get copied just before rendering previous frame. So that parallel update thread can modify new Position, Rotation, Scale, Color.

    Runnable pRunnable;
    Thread pThread;
    public int renderframe = 0;
    piblic int updateframe = 0;

    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {
        // I create a new thread in this function        
        pRunnable = new Runnable(){
             public void run(){
                 while(true)
                 {
                     while( renderframe < updateframe )
                     {
                         // WAITING for render to finish so that data does not get corrupterd
                     }
                     // 
                     // I update RenderObject Position, Rotation, Scale, Color members here
                     UpdateGame();
                     updateframe++;
                 }  
             }
        };
        
        pThread = new Thread(pRunnable);
        pThread.start();
    }

    public void onDrawFrame(GL10 gl) 
    {
        while( renderframe == updateframe )
        {
             // Wait for update to finish
        }
        
        for(int a=0;a<MAX_RENDER_OBJECTS;a++)
        {
            RenderObject ro = aRenderObjects[a];
            // I do this in a function
            ro.RenderPosition[0] = ro.Position[0];
            ro.RenderPosition[1] = ro.Position[1];
            ro.RenderPosition[2] = ro.Position[2];
            ro.RenderPosition[3] = ro.Position[3];
            // I do the same for Rotation, Scale, Color

        }
        
        renderframe++;

        // When rendering scene I use RenderPosition, RenderRotation members when calling OpenGL API
        RenderScene(); 

    }

But when I run my game. The graphics are incorrect. It appears as if data is not in sync.

Any suggestion there on how to appropriately sync RenderObject data between Update and Render threads.

Thank you.