Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the type of dispatch queue (serial or concurrent)?

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?

like image 776
teerapap Avatar asked Sep 13 '12 04:09

teerapap


2 Answers

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

like image 97
ipmcc Avatar answered Oct 22 '22 12:10

ipmcc


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

like image 16
das Avatar answered Oct 22 '22 12:10

das