Note that the main queue does not match the main thread. You can easily work with a non-primary queue in the main thread when dispatch_sync () is used in the queue from the main thread. More rarely, in command-line tools that use dispatch_main () instead of NSRunLoops (that is, except for Cocoa / iOS applications), the main queue can be run in something other than the main thread.
If you are dealing with code that requires the main queue, and not just the main thread (say, code that expects the values ββset in the main queue from queue_get_specific, which, according to rumors, VectorKit / MapKit should do), it is better to check for the presence of the Main the queue is explicit, not the main thread.
One of the options for explicitly checking the main queue:
BOOL MyIsMainQueue(void) { static char MAIN_IND_KEY; static char MAIN_IND_VAL; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dispatch_queue_set_specific(dispatch_get_main_queue(), &MAIN_IND_KEY, &MAIN_IND_VAL, NULL); }); return dispatch_get_specific(&MAIN_IND_KEY) == &MAIN_IND_VAL; }
An answer using DISPATCH_CURRENT_QUEUE_LABEL should also work, and probably better, although it may not work (that is, hang) until MacOS 10.9 and iOS7 when DISPATCH_CURRENT_QUEUE_LABEL is defined.
Carl Lindberg
source share