How to answer
GCD (libdispatch) is Apple's C-level concurrency library: you submit closures to dispatch queues, and the system runs them on a thread pool it manages. Queues are serial or concurrent; DispatchQueue.main is serial and runs on the main thread, and the global queues are concurrent, one per QoS class. In Swift 6 code, GCD is legacy or interop: new code uses Swift Concurrency (@MainActor, actors, @concurrent functions, task groups), which the compiler can check for data races. GCD is still the right tool for APIs that take a queue and for DispatchSource timers and file events.
Key points
DispatchQueue.main(serial, main thread),DispatchQueue.global(qos:)(concurrent), custom queues viaDispatchQueue(label:attributes:)asyncreturns immediately;syncblocks the caller until the work finishes (neversynconto the current queue)- Tools:
DispatchGroup, barriers,DispatchWorkItem,DispatchSemaphore,DispatchSource,asyncAfter - Swift 6: closures passed to
asyncare@Sendable, so captured values must beSendable; hop to main for UI work - GCD is legacy, not deprecated: it builds cleanly in Xcode 27, but completion-handler code that sends non-
Sendableclosures is an error in Swift 6 mode. Modern Swift:DispatchQueue.main.async→@MainActor;DispatchQueue.global().async→@concurrent func; group →async let/ task group;asyncAfter→Task.sleep(for:)
If they dig deeper
GCD avoids thread management but not thread explosion: blocking many items on a concurrent queue makes GCD spawn more threads. Swift Concurrency's cooperative pool runs at most one thread per core and relies on tasks never blocking. Target queues let you funnel many queues into one serial queue. Under the hood, the main actor's executor is the main queue, which is why @MainActor and DispatchQueue.main interoperate.
Common pitfalls
DispatchQueue.main.sync from the main thread (deadlock or trap). Defaulting everything to the global queue with no QoS. Capturing non-Sendable state in dispatch closures (now a compile error in Swift 6). Mixing GCD and async/await by blocking a task with a semaphore. Reaching for DispatchQueue.main.async inside code that's already @MainActor.
How to use it
import UIKit
nonisolated enum FeedParser { // called from a background queue
static func summary() -> String { "12 new posts" } // slow, CPU-bound
}
// Legacy GCD: background work, then hop to main for UI
@MainActor
final class FeedViewController: UIViewController {
let label = UILabel()
func reload() {
DispatchQueue.global(qos: .userInitiated).async {
let summary = FeedParser.summary() // String is Sendable
DispatchQueue.main.async {
self.label.text = summary
}
}
}
}Modern Swift: the Task stays on the main actor; the @concurrent function runs off it.
import UIKit
enum Parser {
@concurrent static func summary() async -> String { "12 new posts" }
}
@MainActor
final class ModernFeedViewController: UIViewController {
let label = UILabel()
private var reloadTask: Task<Void, Never>?
func reload() {
reloadTask?.cancel() // owned, so it can be cancelled
reloadTask = Task { label.text = await Parser.summary() }
}
}Best practices
- Use Swift Concurrency for new code; GCD for queue-based C/ObjC APIs,
DispatchSource, and existing code - Give queues a QoS and a reverse-DNS label (it shows up in the debugger and crash logs)
- Never
synconto the current queue or onto main from main - Keep dispatch closures capturing only
Sendablevalues, and touch UI only on main
Related concepts
- Serial vs concurrent queues, QoS, DispatchGroup, dispatch barriers, DispatchWorkItem, OperationQueue, thread vs queue, @MainActor, @concurrent, deadlock