Why did Apple refuse dispatch_get_current_queue?

Why did Apple refuse dispatch_get_current_queue? What is unsafe in this call?

+8
objective-c grand-central-dispatch
source share
2 answers

dispatch_get_current_queue never made any sense in the first place. Here's why: There are several “root” queues (one for each priority, and then for the main queue). Each successive queue ultimately targets one of these root queues. This means that in general there is no answer to the question "What queue am I starting?"

For example, if you have queue B that is aimed at queue A, then A or B will be a reasonable answer to this question for a block sent to queue B. In addition, since all queues ultimately target one of the global / root perhaps the best answer would be "any root queue that caused it to finish executing", except that it is not very useful to anyone because it does not significantly distinguish anything.

In my experience, in most cases, what people want from dispatch_get_current_queue is the answer to the question: "What kind of queue was I originally introduced to?" However, by definition, whatever code represented by the block no longer knows which queue it was sent to (since it executes the presentation). Therefore, perhaps, if you needed to capture this information, you could do it with the least amount of time; you do not need dispatch_get_current_queue to answer this question. For these cases, dispatch_get_current_queue will be just a shortcut and a disadvantage in this (due to queue targeting).

Another big class of cases is when you want to know if you are in the main line. -[NSThread isMainThread] is sufficient / authoritative for this, so you do not need dispatch_get_current_queue for this.

Another respondent also noted that dispatch_get_current_queue often abused when trying to emulate a recursive lock with GCD queues. It is not possible to reliably implement recursive locks on a queue-based system because "queues are not locks." I wrote several times about this particular situation in another answer .

+14
source share

This may be due to improper use of the method.

Here is a quote from the documentation:

Recommended only for debugging and logging : the code should not make any assumptions about the returned queue, unless it is one of the global queues or the queue created by the code itself. The code should not assume that synchronous execution in a queue is safe deadlock if this queue is not the one returned by dispatch_get_current_queue ().

The same thing happened with setFlipped: in NSImage - Apple rejected them because programmers used it “incorrectly”:

The inverted image property was widely underestimated and outdated.

+2
source share

All Articles