Created
July 15, 2018 03:41
-
-
Save lilyball/8a2eb3224fab0d1b8b795423d5d0f836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func update(_ arg: inout String) { | |
arg = "UPDATED \(arg)" | |
} | |
let mainQueue = DispatchQueue.init(label: "mainQueue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem, target: nil) | |
let workerQueue = DispatchQueue.init(label: "workerQueue", qos: .userInitiated, attributes: [.concurrent], autoreleaseFrequency: .workItem, target: nil) | |
let systemQueue = DispatchQueue.init(label: "systemQueue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem, target: nil) | |
print("Sample Started") | |
mainQueue.async { | |
let group = DispatchGroup() | |
var dict = Dictionary<Int, String>() | |
let end = 1000000 | |
dict.reserveCapacity(end) | |
let range = (0..<end) | |
range.forEach { dict[$0] = $0.description } | |
group.enter() | |
systemQueue.async { | |
let cop = dict | |
range.forEach { id in | |
group.enter() | |
workerQueue.async { [x = cop[id]!] in | |
var x = x | |
update(&x) | |
mainQueue.async { | |
dict[id] = x | |
group.leave() | |
} | |
} | |
} | |
group.leave() | |
} | |
group.notify(queue: DispatchQueue.main, execute: { | |
let finished = dict.values.filter { $0.hasPrefix("UPDATED ") } | |
print(finished.count) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment