Skip to content

17 — concurrentPerform

rebeloper edited this page Jul 12, 2026 · 1 revision

🍽️ Intuition

Instead of one chef chopping 1000 vegetables one at a time, split the pile across every available chef and let them all chop in parallel — but don't plate the dish until every chef is done.

🧠 What It Means In Swift

DispatchQueue.concurrentPerform(iterations:) runs a closure iterations times in parallel across the thread pool, blocking the calling thread until every iteration completes. Best suited for CPU-bound, data-parallel work with no shared mutable state between iterations.

📊 ASCII Diagram

[vegetables] ──split──▶ chef1: chunk1
                         chef2: chunk2      ──▶ all done ──▶ continue
                         chef3: chunk3

💻 Tiny Swift Example

DispatchQueue.concurrentPerform(iterations: vegetables.count) { index in
    chop(vegetables[index]) // must be safe to run out of order, in parallel
}
print("All chopped, this line runs only after every iteration finishes")

🔄 vs. Swift Concurrency

A TaskGroup iterating and awaiting all children achieves similar parallel iteration, but non-blocking (the calling task suspends instead of blocking a thread) and each child task is individually cancelable.

🧸 Memory Sentence

concurrentPerform is "split the pile across every chef, wait for all of them."

⚠️ Analogy Limit

Unlike handing off physical vegetables, iterations that share mutable state can race with each other — concurrentPerform doesn't isolate iterations from one another.

✅ Check Your Understanding

Why is concurrentPerform a poor fit for iterations that depend on each other's results?

Clone this wiki locally