在 swift 3 中在 GCD 中发出并发队列

issues concurrent queue in GCD in swift 3

我在 swift 3 中遇到 GCD 问题 我创建并发队列并在此队列中传递函数此函数调用另一个函数 我需要打印每次通话的经过时间 但我认为实现在并发队列中被削减了以下我的代码:

// peform task with the concurrent queue
class DoCalculations{
    func doCalc() {
        let x = 100
        let y = x * x
        _ = y / x

    }

    func performCalculation(itretion:Int,tag:String) {
        let start = CFAbsoluteTimeGetCurrent()

        for _ in 0..<itretion {
            self.doCalc()
        }

        let end = CFAbsoluteTimeGetCurrent()

        print("tag :\(tag) : \(end - start)")

    }
}


let calc = DoCalculations()

let cQueue = DispatchQueue(label: "com.myCompany", attributes: .concurrent)

cQueue.async {
    calc.performCalculation(itretion: 1000000, tag: "sync1")
}
cQueue.async {
    calc.performCalculation(itretion: 1000, tag: "sync2")
}

cQueue.async {
    calc.performCalculation(itretion: 100000, tag: "sync3")
}

//打印功能未执行 请解决这个问题

如果你在操场上这样做,你想表明执行应该继续"after the end of the playground’s top-level code is reached."你这样做 needsIndefiniteExecution:

import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

正如 needsIndefiniteExecution 的文档所说:

A Boolean value that indicates whether indefinite execution is enabled. By default, all top-level code is executed, and then execution is terminated. When working with asynchronous code, enable indefinite execution to allow execution to continue after the end of the playground’s top-level code is reached. This, in turn, gives threads and callbacks time to execute.

Editing the playground automatically stops execution, even when indefinite execution is enabled.

Set needsIndefiniteExecution to true to continue execution after the end of top-level code. Set it to false to stop execution at that point. The default value is false. It is set to true when liveView is set to a non-nil value.