checking for equality on dispatch_queue_t
Asked Answered
S

3

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?

Socialite answered 26/9, 2012 at 16:20 Comment(0)
S
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.

Superintendent answered 26/9, 2012 at 18:18 Comment(1)
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.Chirk
J
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);
Juxtapose answered 22/4, 2014 at 13:15 Comment(4)
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.Volkman
Is there an advantage to using one or the other (label or specific)?Matriarchate
@AlfieHanssen not sure 100% but think that there is no much performance difference, ok to use what is much convenient for youJuxtapose
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...Cowling
S
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.

Superintendent answered 26/9, 2012 at 18:18 Comment(1)
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.Chirk
M
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.

Mathia answered 27/9, 2012 at 2:0 Comment(2)
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?Colwell
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.Mopes

© 2022 - 2024 — McMap. All rights reserved.