How to determine the type of send queue (serial or parallel)?

A queue is the argument of the caller. I want to know the type (sequential or parallel) of the input dispatch_queue_t, because I will handle it differently.

Is it possible? and how to check it?

+6
source share
2 answers

If all you have is dispatch_queue_t , which was passed to you by someone else, you do not know. This information is effectively hidden from you. If you create the queues yourself, you can use dispatch_queue_set_specific and dispatch_queue_get_specific to put the value in the queue context data and then read it back, but if you do not create the queue, you are lucky.

FWIW, that kind of hint of a fragile design / anti-pattern. Taking a queue as a parameter, you must plan the blocks for future execution in this queue. From this point of view, it does not matter whether the queue is parallel or sequential.

Moreover, your code should be written in such a way that it does not matter if it runs in a sequential or parallel queue. If it uses shared resources, then it must synchronize access to these resources, so if it should be executed in a parallel queue, access to these resources will be safe. Conversely, avoid situations where starting in a sequential queue will be a problem (i.e., do not try to achieve recursive locks with dispatch_sync with a queue that may be sequential.)

+8
source

The idiomatic way to guarantee serialized execution on an arbitrary queue provided by the caller in the GCD is to create your own sequential queue and set the queue provided by the caller as the target queue of the queue (using the dispatch_set_target_queue (3) API).

+10
source

Source: https://habr.com/ru/post/925273/


All Articles