Skip to content

13 — Target Queues

rebeloper edited this page Jul 12, 2026 · 2 revisions

🍽️ Intuition

Several small order lines — one per waiter — all ultimately funnel into the same expedite station. Even though each waiter has their own line, orders from every waiter still get serialized through that one shared point before reaching the chefs.

🧠 What It Means In Swift

A DispatchQueue can target another queue via its target: parameter. Work submitted to the first queue doesn't actually run in its own context — it executes on the target queue's context instead. This lets you funnel multiple independent queues through one shared serial queue for coordinated serialization, or route work onto a specific QoS-tier global queue without creating a new thread pool.

📊 ASCII Diagram

 sub1 ──┐
        ├──▶ [ shared serial queue ] ──▶ expedite station
 sub2 ──┘

💻 Tiny Swift Example

let shared = DispatchQueue(label: "shared-serial")
let sub1 = DispatchQueue(label: "sub1", target: shared)
let sub2 = DispatchQueue(label: "sub2", target: shared)

sub1.async { print("From sub1, serialized through shared") }
sub2.async { print("From sub2, serialized through shared") }

👉 Even though sub1 and sub2 are separate queue objects, work submitted to either one is serialized through shared — no two blocks from sub1 and sub2 ever run at the same time.

🔄 vs. Swift Concurrency

There's no direct equivalent — actor executors can be customized, but that's a different mechanism aimed at controlling where an actor runs, not at funneling multiple independent queues into one shared serialization point.

🧸 Memory Sentence

Target queues let many order lines funnel through one expedite station.

⚠️ Analogy Limit

Unlike a physical expedite station, target queue chains can be arbitrarily deep — a queue can target a queue that targets another queue. That flexibility can make debugging execution order harder than the analogy suggests.

✅ Check Your Understanding

What happens to two independent serial queues if they both target the same serial queue?

Clone this wiki locally