-
Notifications
You must be signed in to change notification settings - Fork 0
13 — Target Queues
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.
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.
sub1 ──┐
├──▶ [ shared serial queue ] ──▶ expedite station
sub2 ──┘
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.
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.
Target queues let many order lines funnel through one expedite station.
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.
What happens to two independent serial queues if they both target the same serial queue?