4

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) {

}

from the docs:

typedef struct dispatch_queue_s *dispatch_queue_t;

I'm not sure but does this mean that it's a pointer to a dispatch_queue_s struct?

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?

Monolo
  • 18,205
  • 17
  • 69
  • 103
the Reverend
  • 12,305
  • 10
  • 66
  • 121

3 Answers3

24

Since dispatch_get_current_queue() is deprecated, we could compare current and your queues by their labels (or specifics as @jkh suggested)

For label use

dispatch_queue_get_label(dispatch_queue_t queue);

and pass DISPATCH_CURRENT_QUEUE_LABEL for get label of current queue

For specific:

dispatch_queue_get_specific(dispatch_queue_t queue, const void *key);

for get you queue specific and

dispatch_get_specific(const void *key);

for current

It's require to set one or both of label and specific for your queue. For example when you create it

dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);

or using setters for specific

dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
    void *context, dispatch_function_t destructor);
alexey.metelkin
  • 1,309
  • 1
  • 11
  • 20
  • This was a huge help. I used `dispatch_queue_set_specific` with a simple boolean flag when I created my queue and `dispatch_get_specific` to see if that flag was set. Worked like a charm. – pottedmeat Jul 25 '14 at 21:25
  • Is there an advantage to using one or the other (label or specific)? – Alfie Hanssen Jun 12 '15 at 17:36
  • @AlfieHanssen not sure 100% but think that there is no much performance difference, ok to use what is much convenient for you – alexey.metelkin Jun 13 '15 at 20:24
  • Interesting. I had no idea about `DISPATCH_CURRENT_QUEUE_LABEL`, the docs don't mention it. In fact, the docs says `queue` can't be `NULL`, but the header file says `DISPATCH_CURRENT_QUEUE_LABEL` is a valid argument for `queue`, and defines it as `NULL`... – skagedal Apr 27 '16 at 07:01
2

This depends on the queue you're on. In this particular case use:

if ([NSThread isMainThread]) {}

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:

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.

leo
  • 7,518
  • 1
  • 24
  • 25
  • That quote does say that you can use `dispatch_get_current_queue` “ to test the identity of the current queue”. The catch is that it only works as expected within a dispatched block. – Peter Hosey Sep 26 '12 at 21:46
2

First part of answer: What are you trying to do? Why do you need to compare queues? If all you need to do is "tag" a queue with some specific piece of metadata, consider using dispatch_queue_{set, get}_specific() instead.

Second part of answer: Don't use dispatch_get_current_queue() for, well, anything. It's just for debugging purposes and its use has always been discouraged.

jkh
  • 3,246
  • 16
  • 13
  • 1
    Would this be why `dispatch_get_current_queue()` is marked as deprecated in the iOS 6.0 headers (although oddly not in the Mountain Lion ones)? I had been using it in a wrapper function to run a block synchronously on a serial queue, but not deadlock if that function happened to be called from within that queue itself. I had assumed that we could use it to perform a check of the identity of the dispatch queue that the function was called from. I take it that's not a safe practice? – Brad Larson Oct 09 '12 at 00:52
  • That's not a safe practice in general, because queue A could make a synchronous dispatch into queue B, which could then try to dispatch back into queue A, and that approach wouldn't detect the deadlock. – dgatwood Jun 11 '18 at 21:43