Member-only story
GCD (Grand Central Dispatch) iOS
Grand Central Dispatch (GCD) is Apple’s answer to multithreading. It is a lightweight framework for performing tasks synchronously or asynchronously in queues and handles CPU threads for you behind the scenes.
Related Topic: Concurrency
Dispatch Semaphore
DispatchSemaphore provides an efficient implementation of a traditional counting semaphore, which can be used to control access to a resource across multiple execution contexts.
A scenario for when to use a semaphore could be if you are doing some file reading/writing, if multiple tasks are trying to read and write from file at the same time, it could increase your performance to make each task wait its turn so as to not overburden the I/O controller.
Swift 3
func do2TasksAtATime () {
print("starting long running tasks (2 at a time)")let sem = DispatchSemaphore(value: 2)for i in 0...7 {DispatchQueue.global().async {sem.wait()
sleep(2)
sem.signal()
}
}
}print("long task \(i) done! \(Date())")
sem.signal()//let the semaphore know this resource is now
Example output: (notice the time stamps)
starting long running tasks (2 at a time)
long task 0 done! 2017-02-16 07:11:53 +0000
long task 1 done! 2017-02-16 07:11:53 +0000
long task 2 done! 2017-02-16 07:11:55 +0000
long task 3 done! 2017-02-16 07:11:55 +0000
long task 5 done! 2017-02-16 07:11:57 +0000
long task 4 done! 2017-02-16 07:11:57 +0000
long task 6 done! 2017-02-16 07:11:59 +0000
long task 7 done! 2017-02-16 07:11:59 +0000