A counter incremented 1000 times from multiple threads: fix it three ways
Task
Why is the result below 1000, and how do you fix it?
var counter = 0
DispatchQueue.concurrentPerform(iterations: 1000) { _ in
counter += 1
}
print(counter) // 987? 994? — different every time
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep