i have an problem with GCD in swift 3 i create the concurrent queue and i pass function in this queue this function call another function i need to print the elapsed time for each call but i think the implementation is cut within the concurrent queue the following my code :
// 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")
}
// the print function is not Excuted please can solve this issues
If you're doing this on a playground, you want to indicate that execution should continue "after the end of the playground’s top-level code is reached." You do this with needsIndefiniteExecution
:
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
As the documentation for needsIndefiniteExecution
says:
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
totrue
to continue execution after the end of top-level code. Set it tofalse
to stop execution at that point. The default value isfalse
. It is set to true whenliveView
is set to a non-nil
value.