Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 949
Charles Avatar asked Oct 29 '22 19:10

Charles


1 Answers

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)
        }
    }
like image 120
Aderstedt Avatar answered Nov 15 '22 07:11

Aderstedt