I am trying to enforce synchronization between a pair of Android threads for game programming purposes. I have assigned a game thread, which handles most duties, and a render thread, which is tasked with swapping buffers and rendering. When I first asked about thread synchronization, I was referred to the ConditionVariable object as a useful tool to force threads to block until concurrent tasks are completed.

My source code looks like this:

            ...
            final ConditionVariable bufferLock = new ConditionVariable();
            final ConditionVariable cycleLock = new ConditionVariable();

            bufferLock.open();
            cycleLock.open();

            Runnable bufferSwapTask = new Runnable()
            {
                public void run()
                {
                    swapBuffers();
                    bufferLock.open();
                }
            };

            Runnable renderTask = new Runnable()
            {
                public void run()
                {
                    Log.d(TAG, "drawAll");
                    drawAll();
                    cycleLock.open();
                }
            };

            while(!halt)
            {
                if(!init)
                {
                    synchronized (userInputLock)
                    {
                        fetchUserInput();
                    }

                    processUserInput();
                    gameLogic();

                    bufferLock.block();
                    cycleLock.close();
                    renderThreadHandler.post(renderTask);
                    recycleDisplayObjects();
                    enqueueDisplayTree();

                    cycleLock.block();
                    bufferLock.close();
                    renderThreadHandler.post(bufferSwapTask);
                }
            }
            ...

So things executed in the right order, but not with the level of performance I had expected. And, when I activated DDMS method tracing, I found that the DVM would actually interrupt and block each thread to allow the other thread to resume, switching back and forth in a manner that strongly suggests that both threads are only being processed by one CPU.

I have had nice simultaneous processing results using ReentrantLocks, so why does ConditionVariable have this effect?

有帮助吗?

解决方案

The Linux kernel on Android tries to avoid moving threads between cores. If a thread is "runnable" (i.e. could run but is waiting on another thread) for a while, the kernel can decide to migrate it to another core.

If, in the previous implementation, one of your threads tended to run continuously, it may have kept the other thread in "runnable" long enough to cause the kernel to migrate it. The new implementation might be moving in smaller steps and fall below the threshold.

FWIW, other people have been puzzled by this, e.g. here and here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top