Swift app using DispatchQueue.concurrentPerform(iterations:) no longer runs concurrently under Mac OS Sierra

时光总嘲笑我的痴心妄想 提交于 2019-12-10 14:23:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!