In GCD, when do I use a global concurrent queue vs a custom concurrent queue?

Bil*_*ill 5 macos grand-central-dispatch ios

In GCD, there are two ways I can run blocks concurrently.

I can use one of the global pools:

DispatchQueue.global().async() {
  // do work
}
Run Code Online (Sandbox Code Playgroud)

or I can create my own queue:

let queue = DispatchQueue(label: "process images", attributes: [.concurrent])
queue.async {
  // do work
}
Run Code Online (Sandbox Code Playgroud)

but I can't find much information on when to prefer one over the other.

Some places (including this summary of mailing list posts from the libdispatch maintainer) suggest that you really shouldn't use the global queues.

Yet most code examples just dispatch to a global queue, and there are even some sources that say you really shouldn't use custom queues - and should prefer the global queues.

每种队列哪种情况更好?与此相关的是,有些文章建议优先使用串行队列而不是并发队列-但显然它们具有完全不同的并行性,因此很奇怪地看到它们建议可互换。

Ken*_*ses 5

除了担心效率和线程爆炸,有了自己的并发队列,你还可以:

  • 指定对调试有意义的标签
  • 暂停它
  • 设置和获取特定于应用程序的数据
  • 提交障碍任务

对于全局并发队列,这些都不可能。

  • 好吧,Apple 表示,如果没有必要,我们应该避免使用自定义并发队列:[避免过多的线程创建](https://developer.apple.com/documentation/dispatch/dispatchqueue) (2认同)