Semaphore.wait(timeout:.now())的目的是什么? [英] What is the purpose of Semaphore.wait(timeout: .now())?

查看:313
本文介绍了Semaphore.wait(timeout:.now())的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过查看一些Apple代码示例,我发现了这一点:

Looking at some Apple code sample, I found this:

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    // wait() is used to drop new notifications if old ones are still processing, to avoid queueing up a bunch of stale data.
    if metadataObjectsOverlayLayersDrawingSemaphore.wait(timeout: .now()) == .success {
        DispatchQueue.main.async {
            // Some processing...

            self.metadataObjectsOverlayLayersDrawingSemaphore.signal()
        }
    }
}

代码上下文 :这是使用视频捕获来检测QR码(或任何其他代码)时的委托方法. 因为每秒触发多次(如果相机停留在相同的QR码上),则需要某种超时.

Context of the code: This is the delegate method when using video capture to detect a QR code (or any other code). Because it is triggered many times per second (if the camera stays on the same QR code), some kind of timeout is needed.

但是DispatchSemaphore.wait(timeout: .now())如何工作?为什么要检查它是否为.success?

But how does DispatchSemaphore.wait(timeout: .now()) work? And why check if it is .success?

推荐答案

目的是评论内容:

wait()用于在仍在处理旧通知时丢弃新通知,以避免排队大量陈旧数据.

wait() is used to drop new notifications if old ones are still processing, to avoid queueing up a bunch of stale data.

,其工作方式如下:

  • 创建的信号量值为1.
  • 第一次调用metadataOutput时,wait(timeout: .now()) 成功并将信号量的值减小为零. 数据开始处理.
  • 如果在之前再次调用metadataOutput,则处理已 完成后,信号量的值为零. 然后wait(timeout:)将等待信号量变为 再次为正,但由于超时值为now(),因此失败 立即返回.timedOut. 结果是传入的数据被忽略,metadataOutput 回调方法会立即返回.
  • 对的数据处理完成后, 信号量被信号发送,将值增加到一. 结果,在 next 回调被调用的时间, 等待信号量将成功,并且将再次处理数据.
  • The semaphore is created with a value of one.
  • When metadataOutput is called the first time, wait(timeout: .now()) succeeds and decrements the value of the semaphore to zero. The processing of the data begins.
  • If metadataOutput is called again before the processing has completed, the semaphore still has a value of zero. Then wait(timeout:) would wait for the semaphore to become positive again, but since the timeout value is now(), it fails immediately and returns .timedOut. The effect is that the incoming data is ignored, the metadataOutput callback method returns immediately.
  • When the data processing on the has completed, the semaphore is signaled, which increases the value to one. As a consequence, the next time the callback is called, waiting for the semaphore will succeed and the data is processed again.

简而言之:

    如果出现以下情况,
  • wait(timeout: .now())返回.success 在这种情况下,先前提交的区块已表示已完成 提交了一个新块来处理传入的数据.
  • wait(timeout: .now())返回.timedOut,如果先前 提交的块仍在运行,在这种情况下,传入的数据 被忽略.
  • wait(timeout: .now()) returns .success if a previously submitted block has signaled completion, in that case a new block is submitted for processing the incoming data.
  • wait(timeout: .now()) returns .timedOut if a previously submitted block is still running, in that case the incoming data is ignored.

这篇关于Semaphore.wait(timeout:.now())的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆