Swift是否通过Grand Central Dispatch的dispatch_async进行并行编程的构造?

Cha*_*les 2 parallel-processing grand-central-dispatch swift

我有兴趣采用新的Swift编程语言.我维护的代码库通过C++ Grand Central Dispatch扩展(dispatch_async等)广泛使用多核处理.

有没有评论过Swift的人知道这种语言能否以类似的方式访问GCD?

Eri*_*rik 8

是的,尽管我在操场上没有取得太大的成功.在一个真正的项目中它应该工作.所有iOS 8和OS X 10.10文档都显示了GCD方法的Swift语法.

dispatch_once在我目前的项目中用于单身人士,它运作得很好:

NSOperation并且NSOperationQueue都在引擎盖下使用GCD,它似乎在操场上更好用:

let operationQueue = NSOperationQueue()

for index in 1...20
{
    operationQueue.addOperationWithBlock(
    {
        let thread = NSThread.currentThread()
        let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue

        println("Task #\(index) completed on thread #\(threadNumber)")
    })
}
Run Code Online (Sandbox Code Playgroud)

输出:

Task #1 completed on thread #3
Task #2 completed on thread #4
Task #3 completed on thread #5
Task #4 completed on thread #4
Task #5 completed on thread #5
Task #6 completed on thread #3
Task #7 completed on thread #6
Task #8 completed on thread #4
Task #9 completed on thread #5
Task #10 completed on thread #5
Task #11 completed on thread #3
Task #12 completed on thread #6
Task #13 completed on thread #3
Task #14 completed on thread #3
Task #15 completed on thread #6
Task #16 completed on thread #5
Task #17 completed on thread #4
Task #18 completed on thread #3
Task #19 completed on thread #4
Task #20 completed on thread #3
Run Code Online (Sandbox Code Playgroud)