如何在不使用dispatch_get_current_queue()的情况下验证我是否在给定的GCD队列上运行? [英] How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

查看:196
本文介绍了如何在不使用dispatch_get_current_queue()的情况下验证我是否在给定的GCD队列上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我需要一个可以保证在特定串行调度队列上同步执行给定块的函数。有可能从已经在该队列上运行的东西调用这个共享函数,所以我需要检查这种情况,以防止从同步调度到同一队列的死锁。

Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function could be called from something already running on that queue, so I needed to check for this case in order to prevent a deadlock from a synchronous dispatch to the same queue.

我使用以下代码执行此操作:

I used code like the following to do this:

void runSynchronouslyOnVideoProcessingQueue(void (^block)(void))
{
    dispatch_queue_t videoProcessingQueue = [GPUImageOpenGLESContext sharedOpenGLESQueue];

    if (dispatch_get_current_queue() == videoProcessingQueue)
    {
        block();
    }
    else
    {
        dispatch_sync(videoProcessingQueue, block);
    }
}

此函数依赖于<$ c $的使用c> dispatch_get_current_queue()确定运行此函数的队列的标识,并将其与目标队列进行比较。如果匹配,它就知道只是在没有派遣到该队列的情况下运行块内联,因为该函数已经在它上面运行。

This function relies on the use of dispatch_get_current_queue() to determine the identity of the queue this function is running on and compares that against the target queue. If there's a match, it knows to just run the block inline without the dispatch to that queue, because the function is already running on it.

我听说过相互矛盾的事情关于是否适当使用 dispatch_get_current_queue()进行这样的比较,我在标题中看到了这个措辞:

I've heard conflicting things about whether or not it was proper to use dispatch_get_current_queue() to do comparisons like this, and I see this wording in the headers:


建议仅用于调试和记录目的:

Recommended for debugging and logging purposes only:

代码不得对返回的队列做任何假设,
除非它是一个全局队列或一个队列,代码本身就会创建
。如果该队列不是
dispatch_get_current_queue()返回的队列,则代码不能假定同步执行到
队列是安全的死锁。

The code must not make any assumptions about the queue returned, unless it is one of the global queues or a queue the code has itself created. The code must not assume that synchronous execution onto a queue is safe from deadlock if that queue is not the one returned by dispatch_get_current_queue().

此外,在iOS 6.0中(但尚未针对Mountain Lion),GCD标题现在将此功能标记为已弃用。

Additionally, in iOS 6.0 (but not yet for Mountain Lion), the GCD headers now mark this function as being deprecated.

它听起来我不应该以这种方式使用这个功能,但我不确定我应该在它的位置使用什么。对于像上面那样针对主队列的函数,我可以使用 [NSThread isMainThread] ,但是如何检查我是否在我的一个自定义串行队列上运行这样我就可以防止死锁?

It sounds like I should not be using this function in this manner, but I'm not sure what I should use in its place. For a function like the above that targeted the main queue, I could use [NSThread isMainThread], but how can I check if I'm running on one of my custom serial queues so that I can prevent a deadlock?

推荐答案

指定你想使用 dispatch_queue_set_specific(任何标识符)。然后,您可以使用 dispatch_get_specific()检查您的标识符。

Assign whatever identifier you want using dispatch_queue_set_specific(). You can then check your identifier using dispatch_get_specific().

请记住 dispatch_get_specific() 很好,因为它将从当前队列开始,然后如果当前队列中没有设置密钥则向上走。这通常无关紧要,但在某些情况下可能有用。

Remember that dispatch_get_specific() is nice because it'll start at the current queue, and then walk up the target queues if the key isn't set on the current one. This usually doesn't matter, but can be useful in some cases.

这篇关于如何在不使用dispatch_get_current_queue()的情况下验证我是否在给定的GCD队列上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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