信号量 semaphore

2018-11-26  本文已影响0人  小凡凡520

如何让异步方法同步执行

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    let sem = DispatchSemaphore(value: 0)
    
    DispatchQueue.global().async {
        print("test async")
        
        sem.signal()
    }
    
    sem.wait(timeout: DispatchTime.distantFuture)
    
    print("test main")
}


// 输出
test async
test main
上一篇 下一篇

猜你喜欢

热点阅读