每一个.signal
都是'+1',每一个.wait
都是'-1'或block
如记录的那样,演示的代码是
let semaphore = DispatchSemaphore(value: 0)
semaphore.signal() // = 1
semaphore.signal() // = 2
semaphore.signal() // = 3
semaphore.wait() // = 2 - pass
semaphore.wait() // = 1 - pass
semaphore.wait() // = 0 - pass
semaphore.wait() // = -1 - hang - waiting for new signal()
这是来自Apple 文档
您可以通过调用 signal() 方法来增加信号量计数,并通过调用 wait() 或其指定超时的变体之一来减少信号量计数。
@discardableResult func signal() -> Int
Discussion
Increment the counting semaphore. If the previous value was less than zero,
this function wakes a thread currently waiting
func wait()
Discussion
Decrement the counting semaphore. If the resulting value is less than zero,
this function waits for a signal to occur before returning.