I'm learning GCD and got question about semaphore. Here is my code:
class ViewController: UIViewController {
var semaphore: dispatch_semaphore_t! = nil
override func viewDidLoad() {
super.viewDidLoad()
semaphore = dispatch_semaphore_create(0)
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
print("Entering")
self.semaphoreTask()
print(self.semaphore.debugDescription)
}
semaphoreTask()
print(semaphore.debugDescription)
}
func semaphoreTask() {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
for i in 0...1000 {
print(i)
if i == 1000 {
print("i is equal to 10000!")
}
}
dispatch_semaphore_signal(self.semaphore)
}
If I run this code so nothing from semaphoreTask is printed in the console, but if I change
semaphore = dispatch_semaphore_create(0)
to
semaphore = dispatch_semaphore_create(1)
Everything starts work well.
The question is why should I write dispatch_semaphore_create(1) but not 0?
Thank you!