本文介绍了使用DispatchQueue.concurrentPerform(iterations :)的Swift应用程序不再在Mac OS Sierra下同时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Sierra下测试我的代码时,我发现以前处理并发队列的方法不再起作用.

In testing my code under Sierra, I found that the methods that previously handled concurrent queues were no longer working.

在分析我的C ++代码库中的错误时,一位用户提出了一种解决方法,其中涉及显式命名队列声明的目标(请参阅此帖子:),解决了这个问题.

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.

在Swift 3中,以下代码将用于并发执行闭包,但是它表现出与上一篇文章中的C ++示例类似的内容:

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.

任何人都知道我如何利用该帖子中建议的解决方法来解决Swift中的并发"调度问题?

Anyone know how I might leverage the workaround recommended in that post to solve my "concurrent for" dispatch issues in Swift?

推荐答案

以Duemunk的注释为基础,这是我如何在后台队列上并行应用函数performFunction的方法:

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)
        }
    }

这篇关于使用DispatchQueue.concurrentPerform(iterations :)的Swift应用程序不再在Mac OS Sierra下同时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:00