iOS GCD:任何全局队列与具有后台优先级的队列之间的差异(DISPATCH_QUEUE_PRIORITY_BACKGROUND)? [英] iOS GCD: Difference between any global queue and the one with background priority (DISPATCH_QUEUE_PRIORITY_BACKGROUND)?

查看:1937
本文介绍了iOS GCD:任何全局队列与具有后台优先级的队列之间的差异(DISPATCH_QUEUE_PRIORITY_BACKGROUND)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读

I am reading Concurrency Programming Guide and things confuse me.

我看到很多代码调用以下任何后台任务:

I see a lot of code invoking the following for any background task:

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

现在我所说的'background'意思是流行的意思:

Now what I mean by 'background' is the popular meaning:

在主(UI)线程以外的任何地方执行的任何东西

返回具有不同优先级的任何非主线程队列。

So following the docs, the above statement returns any non-main-thread queue with differing priorities.

我的问题是,为什么 DISPATCH_QUEUE_PRIORITY_BACKGROUND 存在?
最近我还看到许多异步任务使用 DISPATCH_QUEUE_PRIORITY_BACKGROUND 专门用于执行后台任务。

My question is - why does then DISPATCH_QUEUE_PRIORITY_BACKGROUND exist? Lately I also see many async tasks using DISPATCH_QUEUE_PRIORITY_BACKGROUND specifically to perform background tasks.

不是通过 DISPATCH_QUEUE_PRIORITY_DEFAULT ,DISPATCH_QUEUE_PRIORITY_LOW DISPATCH_QUEUE_PRIORITY_HIGH 运行非常远离主线程,如果他们使用 dispatch_get_global_queue 返回?

Aren't queues returned with DISPATCH_QUEUE_PRIORITY_DEFAULT, DISPATCH_QUEUE_PRIORITY_LOW or DISPATCH_QUEUE_PRIORITY_HIGH run very much away from main thread, if they are being returned using dispatch_get_global_queue?

他们不是背景队列吗?通过 DISPATCH_QUEUE_PRIORITY_BACKGROUND 服务返回的队列的具体目的是什么?我已经除了我上面提到的流行的意思之外,它不是很清楚。

Aren't they background queues? What specific purpose does a queue returned with DISPATCH_QUEUE_PRIORITY_BACKGROUND serve? I already referred to this but it does not clarify much, other than the popular meaning I mentioned above.

我相信我很混淆的话 - 背景和背景队列。

I am sure I am pretty confused with words - background and background queues. If someone can explain (better, graphically) - will be a great help.

推荐答案

如果你有很多后台任务,CPU或设备的CPU将在所有设备之间共享。大多数时候是正确的事情。如果任务完成时间过长,您可以通过尝试提高效率来解决问题。

If you have many background tasks, the CPU or CPUs of your device will be shared between all of them. Most of the time that is the right thing to do. If a task takes too long to finish, you solve the problem by trying to make it more efficient.

在极少数情况下,您可能需要很长时间才能完成任务,但等待它的确定。所以你给它背景的优先。如果在NORMAL优先级上有任何工作要做,那么该工作将首先完成,并且只有当有一个备用CPU不做任何事情时,才会执行BACKGROUND任务。并且有高优先级的队列;该队列中的任务将首先执行;如果一个特定任务需要尽快完成,即使这意味着其他任务被延迟,也可以这样做。

In very rare cases, you may have a task that takes a long time, but it is Ok to wait for it. So you give it BACKGROUND priority. If there is any work to do at NORMAL priority, that work will be done first, and only when there is a spare CPU doing nothing else, the BACKGROUND task will be performed. And there is the queue with HIGH priority; tasks in that queue will be executed first; you would do that if one specific task needs to be finished as quick as possible even if it means that other tasks are delayed.

从编程逻辑的角度来看,所有三个队列都是相同的。它只是影响操作系统尝试首先完成哪些任务,它不关心那么多。

From a point of view of your programming logic, all three queues are identical. It just affects which tasks the OS tries to finish first, and which it doesn't care about that much.

这篇关于iOS GCD:任何全局队列与具有后台优先级的队列之间的差异(DISPATCH_QUEUE_PRIORITY_BACKGROUND)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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