10.1 · Queues, QoS, barriers, groups, semaphores and sources

GCD is still under a large part of every Apple platform, including the parts Swift concurrency runs on. It is a tool for describing what runs where and when, not a thread API.

Mental model. You do not manage threads. You submit work to queues, and the system decides which threads run it. Ordering and safety come from which queue, not from which thread.

Queues and coordination

Serial and concurrent queues, QoS, targets, and the tools for waiting.

  1. What is the difference between serial and concurrent queues and between sync and async, and what do QoS and target queues do? 10.1.1 · L2
    Two independent choices describe a dispatch. The queue decides how its tasks run relative to each other: a serial queue runs one task at a time, in the order…
  2. How do barriers, groups, semaphores and dispatch sources coordinate work? 10.1.2 · L3
    A barrier block (flags: .barrier) on a concurrent queue waits for all earlier blocks to finish, runs alone, and then lets later blocks resume. That gives the…

When it goes wrong

Thread explosion, priority inversion and how the system helps.

  1. What are thread explosion and priority inversion, and how do QoS and the system help? 10.1.3 · L5
    Thread explosion happens when you submit many blocks to a concurrent queue and those blocks block (waiting on I/O, a lock or a sync call). GCD sees idle CPU…

More in Concurrency