import Foundation let count = 10000 var array1 = [Int?](repeating: nil, count: count) DispatchQueue.concurrentPerform(iterations: count) { idx in array1[idx] = idx } print(array1.flatMap({ $0 }).count == count) // false var array2 = [Int?](repeating: nil, count: count) let queue = DispatchQueue.init(label: "test") DispatchQueue.concurrentPerform(iterations: count) { idx in queue.sync { array2[idx] = idx } } print(array2.flatMap({ $0 }).count == count) // true