ConditionVariable $ P $的同时运行pvents两个线程 [英] ConditionVariable prevents both threads from running simultaneously

查看:145
本文介绍了ConditionVariable $ P $的同时运行pvents两个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一对Android的线程游戏编程目的之间执行同步。我已经分配了一个游戏线程,可处理大部分的职责,以及渲染线程,它的任务是在交换缓存和渲染。当我第一次问线程同步,我交到了ConditionVariable对象作为一个有用的工具,迫使线程阻塞,直到并发任务的完成。

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.

我的源$ C ​​$ C是这样的:

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);
                }
            }
            ...

因此​​,在正确的顺序执行,但不能与我预期的业绩水平的东西。而且,当我启动DDMS方法跟踪,我发现DVM实际上中断并阻止每个线程允许其他线程恢复,在强烈暗示两个线程只能被一个CPU处理的方式来回切换。

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.

我不得不使用ReentrantLocks好的同时处理的结果,所以为什么ConditionVariable有这样的效果?

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

推荐答案

在Android的Linux内核试图避免内核之间移动线程。如果一个线程是可运行(即可以运行,但正在等待另一个线程)一段时间,内核可以决定将其迁移到另一个核心。

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.

如果,在previous实现,你的线程之一趋于连续运行,它可能已经保存在可运行的其他线程足够长以引起迁移它内核。新的实现可能会在较小的步骤被移动和低于阈值。

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,其他人一直不解本,例如<一href=\"http://stackoverflow.com/questions/16304005/multi-threading-for-multi-core-processors/\">here和这里

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

这篇关于ConditionVariable $ P $的同时运行pvents两个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆