线程会自动利用多个CPU内核? [英] Threads automatically utilizing multiple CPU cores?

查看:151
本文介绍了线程会自动利用多个CPU内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的应用程序运行2个线程(如渲染线程和游戏更新线程)。如果它与多核CPU(时下典型值)的移动设备上运行,我可以期待的线程会自动分配给不同的内核时,可能吗?

Assume my application runs 2 threads (e.g. a render thread and a game update thread). If it is running on a mobile device with multi-core CPU (typical nowadays), can I expect that threads are assigned to different cores automatically when possible?

我知道底层的操作系统内核(Android的Linux内核)决定调度。我的问题是我是否需要做的任何其他,以使多核心使用,或者是自动和透明的?

I know the underlying OS kernel (Android linux kernel) decides scheduling. My question is whether I need to do anything additional to enable multi-core usage, or is it automatic and transparent?

推荐答案

您需要做的是让两个线程尽可能独立运行。如果你有两个线程它们总是等待对方,他们可能会在同一个内核上运行以节省电力。 (因为它可能看起来没有什么可通过不必使用内核来获得)

What you need to do is to allow the two threads to run independently as possible. If you have two threads which are always waiting for each other, they might run on the same core to save power. (Because it might appear there is nothing to be gained by having to cores being used)

的情况是这样的:第一个线程去codeS位图,而渲染器上传previous位图GPU的纹理。第一个线程进入睡眠状态,如果没有做;再醒来时,如果一个位图需要再次去codeD。

the case is the following: first thread decodes a bitmap, while renderer uploads the texture of the previous bitmap to GPU. The first thread goes to sleep if nothing to do; then wakes up if a bitmap needs to be decoded again.

我怀疑这是一个很好的例子,其中两个线程不会帮助,因为位图解码应该比上传这意味着你有两种情况更快

I suspect this is a good example where two threads won't help because decoding the bitmap should be faster than "uploading" This means you have two situations

T1: decoding bit map, 
T2: waiting for a bit map.

T1: sleeping
T2: uploading a bit map.

T1: sleeping
T2: waiting for a bitmap.

你可以看到有没有情况下两个线程都需要运行(或者很少),这可能是没有不仅仅是这样快,甚至慢

Can you see how there is no situation where both threads are needing to run (or perhaps rarely) This may be no faster, or even slower than just doing this

T1: decodes bitmap.
    uploads bitmap.
    waiting for a bitmap.

这篇关于线程会自动利用多个CPU内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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