December 2018
Intermediate to advanced
414 pages
10h 19m
English
In the following example, we'll use a semaphore to suspend the execution of the current thread until a long-running task has completed:
let semaphore = DispatchSemaphore(value: 0)let queue = DispatchQueue(label: "com.run.concurrent", attributes: .concurrent)// Run a block 1 second of the futurequeue.asyncAfter(deadline: DispatchTime.now() + 1) { print("Will Signal") semaphore.signal() print("Did Signal")}print("Will Wait")// wait for the semaphore, this suspends the current threadsemaphore.wait()print("Done")
This will give us the following output:
Will WaitWill SignalDid SignalDone
As you can see, this code successfully waited till the long-running operation was done, and signal() has been called on the semaphore. ...
Read now
Unlock full access