The queue is an argument from the caller. I want to know the type (serial or concurrent) of the input dispatch_queue_t because I'll handle it differently.
Is it possible? and how to check it?
If all you have is a dispatch_queue_t
that was passed to you by "someone else", there is no way for you to know. That information is effectively hidden from you. If you are creating the queues yourself, then you could use dispatch_queue_set_specific
and
dispatch_queue_get_specific
to stash a value in the queue's context data, and then read that back out, but if you're not creating the queue, you're outta luck.
FWIW, this sort of hints at a brittle design/anti-pattern. Taking a queue as a parameter implies that you would schedule blocks for future execution on that queue. From that perspective it shouldn't matter whether the queue is concurrent or serial.
More to the point, your code should be written such that it doesn't matter if it's executed on a serial or concurrent queue. If it uses shared resources, then it should synchronize access to those resources such that if it were to be executed on a concurrent queue, access to those resources would be safe. Conversely, avoid situations in which running on a serial queue would be a problem (i.e. don't try to achieve recursive locks by using dispatch_sync
with a queue that might be serial.)
The idiomatic way to guarantee serialized execution on an arbitrary caller-provided queue in GCD is to create your own serial queue and set the caller-provided queue to be your queue's target queue (using the dispatch_set_target_queue(3) API).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With