Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created July 15, 2018 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilyball/8a2eb3224fab0d1b8b795423d5d0f836 to your computer and use it in GitHub Desktop.
Save lilyball/8a2eb3224fab0d1b8b795423d5d0f836 to your computer and use it in GitHub Desktop.
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