Why did Apple deprecate dispatch_get_current_queue? [closed] Why did Apple deprecate dispatch_get_current_queue? [closed] objective-c objective-c

Why did Apple deprecate dispatch_get_current_queue? [closed]


dispatch_get_current_queue never really made sense in the first place. Here's why: There are a handful of "root" queues (one for each priority, and then the main queue). Every other queue ultimately ends up targeting one of these root queues. This means that, in the general case, there isn't a single answer to the question, "What queue am I running on?"

For instance, if you have queue B that targets queue A, then either A or B would be a reasonable answer to that question, for a block submitted to queue B. Furthermore, since all queues end up targeting one of the global/root queues, arguably the best answer would be "whatever root queue it ended up executing on", except that's not really useful to anyone, because it doesn't significantly differentiate anything.

In my experience, in most cases, what folks want from dispatch_get_current_queue is the answer to, "What queue was I initially submitted to?" However, by definition, whatever code submitted the block already knows what queue it was submitted to (because it's doing the submission). So arguably, if you needed to capture that information, you could trivially do so at enqueue time; you don't need dispatch_get_current_queue to answer that question. For these cases, dispatch_get_current_queue would just be a shortcut, and a flawed one at that (because of queue targeting.)

The other big class of cases is when you want to know if you're on the main queue. -[NSThread isMainThread] is sufficient/authoritative for that, so you don't need dispatch_get_current_queue for that either.

Another answerer also pointed out that dispatch_get_current_queue was frequently misused in an attempt to emulate recursive locking with GCD queues. It's not possible to reliably implement recursive locks in a queue based system because "queues aren't locks". I've written at some length about that particular situation in another answer.


It might be connected with wrong method usage.

Here is quote from documentation:

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

Same situation was with setFlipped: in NSImage — Apple deprecated them, because programmers used it in "wrong" way:

The flipped property of an image was widely misunderstood and has been deprecated.