Serial vs concurrent queues

Concurrency Junior Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Predict the output

What is the print order?

let serial = DispatchQueue(label: "serial")
print("A")
serial.async { print("B") }
serial.sync { print("C") }
print("D")
Options
Hint 1 of 2

Serial queues are first in, first out.

Hint 2 of 2

Does sync return before its closure runs?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: B A, B, C, D

Correct. B is queued, C waits behind it, and sync blocks until C is done, so D prints last.

Why not the others?
  • A A, C, D, B. B was queued first. A serial queue runs items in order, so C waits for B.
  • C A, D, B, C. sync blocks the caller until C runs, so D can't come before C.
  • D Unpredictable. Serial queues are FIFO, so the order is guaranteed.

30-second answer

Grand Central Dispatch runs closures on queues backed by a shared thread pool. A serial queue runs one item at a time, in order. A concurrent queue can run several at once. async returns immediately; sync waits until the item finishes. New code mostly uses Swift concurrency, but GCD remains common in existing apps.

Practise saying it · 45 s. No recording, just a timer

Explain serial vs concurrent queues and sync vs async.

Say your answer out loud, then check which of these points you covered:

  • Serial: one at a time, in order
  • Concurrent: several at once
  • async returns at once; sync waits
  • Prefer Swift concurrency in new code

Deep dive

The main queue is serial and runs on the main thread.

Global queues are concurrent and chosen by QoS (userInteractive to background).

A private serial queue is a simple way to protect state in pre-concurrency code.

Too many blocked threads cause 'thread explosion'; avoid blocking inside queue work.

Common mistakes

  • Thinking async means parallel on a serial queue.
  • Calling sync on the queue you are already on.
  • Creating a new queue per call.

Senior take & follow-ups

Seniors know GCD's ordering rules precisely and explain why new code should prefer actors.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does QoS affect?

    Show hint

    Scheduling priority and energy use.

  2. How do you wait for several async items?

    Show hint

    DispatchGroup with notify, or move to a TaskGroup.

Suggest a fix if something here is wrong or out of date.