问题
In testing my code under Sierra, I found that the methods that previously handled concurrent queues were no longer working.
In analyzing the error in my C++ codebase, one of the users suggested a workaround that involved explicitly naming a target for the queue declaration (see this post: C++11 app that uses dispatch_apply not working under Mac OS Sierra ) that seems to have solved the problem.
In Swift 3, the following code would be used to execute a closure concurrently, but it is exhibiting a similar but to the C++ example in the above post:
import Foundation
import GameKit
DispatchQueue.concurrentPerform(iterations: 1000) { index in
let pauseTime = GKRandomSource.sharedRandom().nextInt(upperBound: 5)
sleep(UInt32(pauseTime))
print(index)
}
... however, when I execute it, it runs each block serially and the indices are output in numeric order.
Anyone know how I might leverage the workaround recommended in that post to solve my "concurrent for" dispatch issues in Swift?
回答1:
Building on duemunk's comment, here is how I apply a function performFunction
in parallel on a background queue:
let queue = DispatchQueue(label: "myQueue", qos: .userInteractive, attributes: .concurrent)
queue.async {
DispatchQueue.concurrentPerform(iterations: iterations) {
index in
performOperation(index)
}
}
来源:https://stackoverflow.com/questions/39843007/swift-app-using-dispatchqueue-concurrentperformiterations-no-longer-runs-conc