gpt4 book ai didi

iOS GCD : Difference between any global queue and the one with background priority (DISPATCH_QUEUE_PRIORITY_BACKGROUND)?

转载 作者:IT王子 更新时间:2023-10-29 07:43:24 27 4
gpt4 key购买 nike

我正在阅读 Concurrency Programming Guide事情让我感到困惑。

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

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

现在我所说的“背景”是通俗的意思:

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

所以按照文档,上面的语句返回任何具有不同优先级的非主线程队列。

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

队列没有返回 DISPATCH_QUEUE_PRIORITY_DEFAULT , DISPATCH_QUEUE_PRIORITY_LOWDISPATCH_QUEUE_PRIORITY_HIGH如果使用 dispatch_get_global_queue 返回它们,它们会远离主线程运行?

它们不是后台队列吗? DISPATCH_QUEUE_PRIORITY_BACKGROUND 返回的队列的具体用途是什么?服务?我已经referred to this但除了我上面提到的流行含义外,它并没有说明太多。

我敢肯定我对单词 - 后台和后台队列感到很困惑。如果有人可以解释(更好的,图形化的)- 将是一个很大的帮助。

最佳答案

这在 dispatch/queue.h header 中有很好的解释:

DISPATCH_QUEUE_PRIORITY_HIGH Items dispatched to the queue will run at high priority, i.e. the queue will be scheduled for execution before any default priority or low priority queue.

DISPATCH_QUEUE_PRIORITY_DEFAULT Items dispatched to the queue will run at the default priority, i.e. the queue will be scheduled for execution after all high priority queues have been scheduled, but before any low priority queues have been scheduled.

DISPATCH_QUEUE_PRIORITY_LOW Items dispatched to the queue will run at low priority, i.e. the queue will be scheduled for execution after all default priority and high priority queues have been scheduled.

DISPATCH_QUEUE_PRIORITY_BACKGROUND Items dispatched to the queue will run at background priority, i.e. the queue will be scheduled for execution after all higher priority queues have been scheduled and the system will run items on this queue on a thread with background status as per setpriority(2) (i.e. disk I/O is throttled and the thread's scheduling priority is set to lowest value).

请记住这是一个全局队列。其他的东西,比如系统框架,可能会被安排进去。优先级很容易耗尽 - 如果有很多 DISPATCH_QUEUE_PRIORITY_HIGH 任务被调度,默认优先级的任务可能需要等待很长时间才能执行。 DISPATCH_QUEUE_PRIORITY_BACKGROUND 中的任务可能需要等待很长时间,因为在它们之上的所有其他优先级都必须是空的。

很多开发者滥用全局并发队列。他们想要一个执行 block ,需要一个队列,并以默认优先级使用它。这种做法可能会导致一些非常难以解决的错误。全局并发队列是一种共享资源,应该小心对待。在大多数情况下,创建私有(private)队列更有意义。

并发队列不是异步的,它是并发的。同步任务依然可以调度到里面,依然会同步执行。并发队列,如串行队列,以 FIFO 顺序出队。与串行队列不同,它们同时执行 block 。并发和异步不是一回事。

另外请记住,如果主线程空闲,并发队列可以重新使用该线程 - 事实上,它更愿意这样做而不是创建新线程。使用并发队列不会保证您不会阻塞用户界面:

Blocks submitted to these dispatch queues are invoked on a pool of threads fully managed by the system. No guarantee is made regarding which thread a block will be invoked on; however, it is guaranteed that only one block submitted to the FIFO dispatch queue will be invoked at a time.

GCD 不保证将使用哪个线程来执行并发队列上的 block 。如果使用主队列, block 将在主线程上串行执行。并发队列可以使用任何线程,并且作为优化将更喜欢使用现有线程。如果没有线程可重用,它只会创建一个新线程。事实上,主线程通常是首选(如果主线程可用于工作),因为它是“暖线程”。

重申一下:使用 Grand Central Dispatch,您可以确定任务将在主线程上执行(通过提交到主队列)。您不能确定任务不会在主线程上执行。

关于iOS GCD : Difference between any global queue and the one with background priority (DISPATCH_QUEUE_PRIORITY_BACKGROUND)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25052629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com