Why did Apple deprecate dispatch_get_current_queue?

grand-central-dispatch, objective-c

Solution

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

Problem

Why did Apple deprecate dispatch_get_current_queue? What's unsafe about this call?

Original source

Related problems