objective c - What GCD queue, main or not, am I running on? -
i trying write thread safe methods using:
... dispatch_queue_t main = dispatch_get_main_queue(); dispatch_sync(main,^{ [self dosomethingintheforeground]; }); ...
but if on main thread not necessary, , can skip dispatch calls, know thread on. how can know this?
or, perhaps not make difference (in performance) doing it?
is ok comparison?
if (dispatch_get_main_queue() == dispatch_get_current_queue()){...}
updated answer:
the apple docs have changed , "when called outside of context of submitted block, function returns main queue if call executed main thread. if call made other thread, function returns default concurrent queue." checking dispatch_get_main_queue() == dispatch_get_current_queue()
should work.
original answer:
using dispatch_get_main_queue() == dispatch_get_current_queue()
won't work. docs dispatch_get_current_queue
"when called outside of context of submitted block, function returns default concurrent queue". default concurrent queue not main queue.
[nsthread ismainthread]
should work want. note [nsthread ismainthread]
can true for queues other main queue though, e.g., when calling dispatch_sync
main thread.
Comments
Post a Comment