Bill
Bill

Reputation: 45408

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

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

I can use one of the global pools:

DispatchQueue.global().async() {
  // do work
}

or I can create my own queue:

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

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.

Which situations are better for each kind of queue? Somewhat relatedly, some articles advise preferring serial queues over concurrent queues - but obviously they have completely different parallelism characteristics, so it's odd to see them suggested as interchangeable.

Upvotes: 10

Views: 2049

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

In addition to concerns about efficiency and thread explosion, with your own concurrent queue, you can:

  • Specify a label that's meaningful to you for debugging
  • Suspend it
  • Set and get app-specific data
  • Submit barrier tasks

None of these are possible with the global concurrent queues.

Upvotes: 7

Related Questions