检查dispatch_queue_t的相等性 [英] checking for equality on dispatch_queue_t
问题描述
如何检查dispatch_queue_t vars之间的相等性?
How can I check for equality between dispatch_queue_t vars?
dispatch_queue_t currentQueue = dispatch_get_current_queue();
dispatch_queue_t mainQueue = dispatch_get_main_queue();
if (currentQueue == mainQueue) {
}
从文档:
typedef struct dispatch_queue_s *dispatch_queue_t;
我不确定,但这是否意味着它是一个指向dispatch_queue_s结构的指针?
I'm not sure but does this mean that it's a pointer to a dispatch_queue_s struct?
由于我不能检查指针的相等性,我不知道如何检查dispatch_queue_t是否与另一个相同?
Since I can't check equality on pointers, I'm not sure how can I check if a dispatch_queue_t is the same as another?
推荐答案
这取决于你所在的队列。在这种特殊情况下使用:
This depends on the queue you're on. In this particular case use:
if ([NSThread isMainThread]) {}
通常,您可以使用 dispatch_get_current_queue()
来测试您的队列。在这种情况下,您可以使用 ==
运算符。要引用Apple的并发编程指南中的调度队列页面:
In general, you can use dispatch_get_current_queue()
to test which queue your're on. In that case you can use the ==
operator to do so. To quote the Dispatch Queues page in Apple's Concurrency Programming Guide:
使用dispatch_get_current_queue函数进行调试,或者使用
来测试身份的当前队列。在块对象内从
调用此函数返回块是
提交的队列(并且现在可能正在运行它)。从块外部调用此
函数将为应用程序返回默认并发队列
。
Use the dispatch_get_current_queue function for debugging purposes or to test the identity of the current queue. Calling this function from inside a block object returns the queue to which the block was submitted (and on which it is now presumably running). Calling this function from outside of a block returns the default concurrent queue for your application.
这篇关于检查dispatch_queue_t的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!