【发布时间】:2018-10-19 21:06:41
【问题描述】:
我想有效地实现这种行为:
一个函数被要求运行(由用户)。知道这个函数也会被计时器自动重复调用,我想确保函数在它已经运行时返回。
在伪代码中:
var isRunning = false
func process() {
guard isRunning == false else { return }
isRunning = true
defer {
isRunning = false
}
// doing the job
}
我知道信号量的概念:
let isRunning = DispatchSemaphore(value: 1)
func process() {
// *but this blocks and then passthru rather than returning immediately if the semaphore count is not zero.
isRunning.wait()
defer {
isRunning.signal()
}
// doing the job
}
您将如何使用信号量通过信号量或任何其他解决方案来实现此行为?
【问题讨论】:
标签: swift grand-central-dispatch semaphore