Skip to content

02 — Thread vs Queue

rebeloper edited this page Jul 12, 2026 · 1 revision

🍽️ Intuition

A restaurant doesn't let customers walk into the kitchen and grab a chef 🧑‍🍳 by the sleeve.

Orders go through the order line first.

  • Customer places an order 📋
  • Order waits in the line 🧾
  • A chef 🧑‍🍳 — whichever one is free — picks it up and cooks it

👉 The chef is a physical worker. The order line is where orders wait to be picked up.

You never address a chef directly. You hand your order to the line, and the line connects it to whichever chef is available.


🧠 What It Means In Swift

In Swift, these two ideas map to two very different things:

  • Thread / pthread — a raw OS execution unit. This is the chef: an actual worker that runs code, with its own stack and scheduling overhead.
  • DispatchQueue — GCD's abstraction over that worker. This is the order line: a place where you submit blocks of work.

With GCD, you never touch threads directly:

  • You create a DispatchQueue and call .async { ... } to submit a block
  • GCD's shared thread pool picks whichever thread is free and assigns it to run your block
  • You don't pick the thread. You don't know which thread runs your block. You don't need to.

One more twist: it's not one queue per chef. Multiple queues can share the same underlying thread pool — just like multiple order lines in a big restaurant might all draw from the same roster of chefs in the back.

👉 Talking to a queue is not the same as talking to a thread. The queue is where you place your order; the thread pool decides who actually cooks it.


📊 ASCII Diagram

📋 Block 1 ──┐
📋 Block 2 ──┼──> 🧾 Queue ──> 👨‍🍳👩‍🍳🧑‍🍳 (shared thread pool)
📋 Block 3 ──┘                        │
                                       v
                         whichever chef is free picks up the block

💻 Tiny Swift Example

let queue = DispatchQueue(label: "com.example.orders")
queue.async {
    print("Handled by whichever thread GCD assigns 👨‍🍳")
}

👉 You submitted a block to the queue. You have no idea — and don't need to know — which thread actually ran it.


🔄 vs. Swift Concurrency

Swift Concurrency has its own version of "a unit of work you hand off without picking the worker": the Task.

  • A Task is the analogous unit of work — you create one and it runs
  • Like GCD, it executes on a cooperative thread pool you never manage directly
  • You don't pick the thread for a Task any more than you pick the thread for a block on a DispatchQueue

👉 Both GCD and Swift Concurrency agree on one thing: you talk to an abstraction (queue or task), never to a thread.


🧸 Memory Sentence

You talk to the order line, never directly to a chef.


⚠️ Analogy Limit

A real order line is strictly FIFO — first order in, first order out, no exceptions.

GCD's queue-to-thread assignment is smarter — and messier — than that:

  • It factors in Quality of Service (QoS) — higher-priority work can effectively cut ahead
  • It factors in current system load — how busy the thread pool already is

So "the order line" captures you don't talk to chefs directly — but not the fact that the line itself can reorder and prioritize behind the scenes.


✅ Check Your Understanding

Why does GCD hide threads from you instead of letting you create and manage them directly, like the restaurant hides its chefs behind the order line?

Clone this wiki locally