ios - 如果我想让一个任务在后台运行, "dispatch_get_global_queue"队列如何工作?

标签 ios objective-c multithreading grand-central-dispatch nsoperation

当选择在哪个队列上运行dispatch_async时,经常提到dispatch_get_global_queue。这是一个将任务委托(delegate)给某个线程的特殊后台队列吗?它几乎是单例吗?

因此,如果我始终使用该队列进行 dispatch_async 调用,该队列是否会变满并且必须等待任务完成才能开始另一个任务,或者是否可以将其他任务分配给不同的线程?

我想我有点困惑,因为当我为 NSOperation 选择队列时,我可以使用 [NSOperationQueue mainQueue] 选择主线程的队列,这似乎与 dispatch_get_main_queue 同义,但我的印象是 NSOperation 的后台队列必须单独创建 NSOperationQueue 的实例,但 GCD 已经后台队列的单例? (dispatch_get_global_queue)

此外 - 愚蠢的问题,但想确保 - 如果我将任务放入队列中,该队列将分配给一个线程,对吗?如果任务足够大,它不会将其拆分为多个线程,不是吗?

最佳答案

When selecting which queue to run dispatch_async on, dispatch_get_global_queue is mentioned a lot. Is this one special background queue that delegates tasks to a certain thread?

某个线程?编号dispatch_get_global_queue为您检索所请求的相对优先级的全局队列。 dispatch_get_global_queue 返回的所有队列是并发的,并且可以根据系统的判断将工作分派(dispatch)给许多不同的线程。其机制是一个实现细节,对于 API 的使用者来说,它是不透明的。

在实践中,冒着过度简化的风险,每个优先级都有一个全局队列,在撰写本文时,根据我的经验,每个优先级都将在任何给定时间将工作分派(dispatch)到0 和 64 线程。

Is it almost a singleton?

绝对不是,但您可以将它们视为单例,其中每个优先级有一个单例。

So if I use that queue always for my dispatch_async calls, will that queue get full and have to wait for things to finish before another one can start, or can it assign other tasks to different threads?

它可能会满。实际上,如果您的全局并发队列之一已饱和(即同时运行的相同优先级的后台任务超过 64 个),那么您的设计可能很糟糕。 (有关队列宽度限制的更多详细信息,请参阅 this answer)

I guess I'm a little confused because when I'm choosing the queue for an NSOperation, I can choose the queue for the main thread with [NSOperationQueue mainQueue], which seems synonymous to dispatch_get_main_queue

它们并不是严格意义上的同义词。虽然NSOperationQueue在底层使用 GCD,有一些重要的区别。例如,在主运行循环的一次传递中,只有一个操作排队到+[NSOperationQueue mainQueue]。将会被执行,而多个区 block 提交到dispatch_get_main_queue可以在单个运行循环传递上执行。这对您来说可能并不重要,但严格来说,它们不是同一件事。

but I was under the impression background queues for NSOperation had to be individually made instances of NSOperationQueue, yet GCD has a singleton for a background queue? (dispatch_get_global_queue)

简而言之,是的。听起来你正在将 GCD 和 NSOperationQueue 混为一谈。 。 NSOperationQueue不仅仅是 GCD 的“琐碎包装”,它是它自己的东西。它是在 GCD 之上实现的这一事实对您来说并不重要。 NSOperationQueue是一个任务队列,具有显式可设置的宽度,您可以“随意”创建实例。您可以根据需要制作任意多个。在某个时刻, NSOperationQueue 的所有实例是,执行 NSOperations 时,从与进程的其余部分相同的系统资源池中提取资源,包括 GCD,所以是的,那里有一些交互,但它们对您来说是不透明的。

Furthermore - silly question but wanted to make sure - if I put a task in a queue, the queue is assigned to one thread, right? If the task is big enough it won't split it up over multiple threads, will it?

单个任务只能在单个线程上执行。系统并没有什么神奇的方法可以将单一任务“拆分”为子任务。那是你的工作。关于您的具体措辞,队列不是“分配给一个线程”,任务是。队列中要执行的下一个任务可能会在完全不同的线程上执行。

关于ios - 如果我想让一个任务在后台运行, "dispatch_get_global_queue"队列如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21122334/

相关文章:

objective-c - 返回文件夹或卷的卷名称

iphone - 在 Objective-C 中绑定(bind)指针

linux - 检查 fork() 是否安全

ios - 使用 Apple Pencil 流畅绘图

ios - cordova run ios --device 没有向设备启动应用程序

ios - 类型 "character set"没有成员 "utf8"

java - LayerUI仅在阻塞方法完成后才出现

ios - 使用服务帐户从 iOS 应用上传到 Google Cloud Storage

ios - 为什么 Main.storyboard 不显示?

c - 多线程处理一系列方程式