check if on main queue versus on main thread

Can someone explain to me how and why the solution in this article works? http://blog.benjamin-encz.de/post/main-queue-vs-main-thread/ It is a solution to check whether you are running on the main queue. I understand the problem, I just don't understand how using dispatch_queue_set_specific and dispatch_get_specific to retrieve a key-value pair matters in any way.

Answered by QuinceyMorris in 247428022

Uunfortunately, I don't remember where that was, or the exact details of what was said. It might have been on the old developer forums, or one of the developer mailing lists. If was a comment about being careful testing what the "current" queue is, because you might be surprised by the results.


It's something to do with the fact that most dispatch queues are actually references to other dispatch queues. There is a limited set of "base" queues, and if you declare your own queues, they actually target (directly or indirectly) one of the base queues.


If you're interested, I think this 2017 WWDC video discusses the sorts of re-arrangements that go on behind the scenes:


https://developer.apple.com/videos/play/wwdc2017/706/


The optimizations talked about here are new, but IIRC this will give you a "flavor" for the complexity behind GCD.

a. Why do you care? Without some further reason, it's a bug if any API checks it's running on the main queue (rather than the main thread).


b. The fix described here applies to any code you write that needs to check if it's on the main queue (rather than the main thread). Why would you write such code?


c. What the blog says is, because there's no built-in way of recognizing the main queue, you have to set a custom "tag" on it, and on it alone, so that you can recognize it later.

I just wondered because the Ray Wenderlich video on concurrency called attention to that matter. Are you saying this isn't something Apple overlooked but is something that is irrelevant?

I don't know about overlooked, but there is a genuine question about what problem this all solves.


In general, your responsibility (as a developer) is to make sure certain API calls are made on the main thread. There isn't any requirement (AFAIK) that requires you to make API calls on the main queue. If there was, it'd be a bug, almost certainly.


The scenario in the blog where some developer might want to check involves reasoning about the current queue in order to make some decision about which API to call (e.g. to dispatch synchronously vs. to execute directly). I recalled, after my previous post, that Apple has long said publicly that you should not try to reason about the current queue, or you risk exacty the kinds of issues discussed in the blog post.


AFAICT, the discussion in the blog (and perhaps the discussion in Ray Wenderlich's video) is directed at programmers who want to ignore Apple's advice about what not to do. Unless you have a specific reason to dive into this black hole, it seems like a much better idea to avoid it completely.

I'd like to read what exactly Apple said about that. Do you by chance know how I can find a reference to that information? I'd like to understand more about this topic.

Accepted Answer

Uunfortunately, I don't remember where that was, or the exact details of what was said. It might have been on the old developer forums, or one of the developer mailing lists. If was a comment about being careful testing what the "current" queue is, because you might be surprised by the results.


It's something to do with the fact that most dispatch queues are actually references to other dispatch queues. There is a limited set of "base" queues, and if you declare your own queues, they actually target (directly or indirectly) one of the base queues.


If you're interested, I think this 2017 WWDC video discusses the sorts of re-arrangements that go on behind the scenes:


https://developer.apple.com/videos/play/wwdc2017/706/


The optimizations talked about here are new, but IIRC this will give you a "flavor" for the complexity behind GCD.

check if on main queue versus on main thread
 
 
Q