import UIKit
class ViewController: UIViewController {
var semaphore:dispatch_semaphore_t;
required init(coder aDecoder: NSCoder) {
self.semaphore = dispatch_semaphore_create(1)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), {() -> Void in
self.task_first()
})
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { () -> Void in
self.task_second()
})
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), { () -> Void in
self.task_third()
})
}
func task_first(){
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
NSLog("%@","First task starting")
sleep(1)
NSLog("%@", "First task is done")
dispatch_semaphore_signal(self.semaphore)
}
func task_second(){
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
NSLog("%@","Second task starting")
sleep(1)
NSLog("%@", "Second task is done")
dispatch_semaphore_signal(self.semaphore)
}
func task_third(){
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
NSLog("%@","Thrid task starting")
sleep(1)
NSLog("%@", "Thrid task is done")
dispatch_semaphore_signal(self.semaphore)
}
}