What is the GCD line, important or not, am I launching?

I am trying to write some thread safe methods, so I use:

... dispatch_queue_t main = dispatch_get_main_queue(); dispatch_sync(main,^{ [self doSomethingInTheForeground]; }); ... 

But if I am in the main thread, which is not needed, and I can skip all these dispatch calls, so I would like to know which thread I am currently in. How can I know this?

Or maybe it makes no difference (in performance)?

Can this be compared?

 if (dispatch_get_main_queue() == dispatch_get_current_queue()){...} 
+9
multithreading objective-c cocoa grand-central-dispatch
source share
9 answers

Updated answer :

Apple docs have changed and now say: "When called from outside the context of the presented block, this function returns the main queue if the call is made from the main thread. If the call is made from any other thread, this function returns the parallel queue by default." so checking dispatch_get_main_queue() == dispatch_get_current_queue() should work.

Original answer :

Using dispatch_get_main_queue() == dispatch_get_current_queue() will not work. The docs for dispatch_get_current_queue say: "When called outside the context of the presented block, this function returns a parallel parallel queue by default." The default parallel queue is not the main queue.

[NSThread isMainThread] should work the way you want. Note that [NSThread isMainThread] may be true for queues other than the main queue, for example, when dispatch_sync called from the main thread.

+12
source share

Impairment of dispatch_get_current_queue() equivalent (dispatch_get_main_queue() == dispatch_get_current_queue())

now compares to the queue label:

 (dispatch_queue_get_label(dispatch_get_main_queue()) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)) 
+11
source share

Updated answer:

dispatch_get_current_queue () is now deprecated.

Original answer:

In OS-X 10.8, the header file (queue.h) in the comment above the dispatch_get_current_queue () function says:

When dispatch_get_current_queue () is called in the main thread, it may or may not return the same value as dispatch_get_main_queue (). Comparing the two is not a valid way of checking whether code is running on the main topic.

I found that because

assert(dispatch_get_current_queue() == dispatch_get_main_queue());

in my code that works well on iOS but doesn't work on OS-X.

+4
source share

If you are in Objective-C and want something to happen on the main thread synchronously, it would not be easier to use

 [self performSelectorOnMainThread: @selector(doSomethingInTheForeground) withObject: nil waitUntilDone: YES]; 

This has the advantage that if you are already in the main thread, it does not matter, the message is sent in the usual way.

+2
source share

You cannot use dispatch_get_current_queue() , except that you are not debugging. As clearly written on the dispatch_queue page :

WARNINGS

The code cannot make any assumptions about the queue returned by dispatch_get_current_queue() . The returned queue may have arbitrary policies that may surprise the code that is trying to schedule the queue. The list of policies includes, but is not limited to, queue width (for example, serial or parallel), scheduling priority, security credentials, or file system configuration. Therefore, dispatch_get_current_queue() MUST be used only for identity testing or debugging.

It's better (maybe harder) to sync background threads:

 dispatch_sync(backgroundqueue,^{ [self doSomethingInTheBackground]; }); 

I may be completely wrong, but this is what I propose.

+1
source share

Actually, this is normal to use.

The documentation states

When the presented block is called from outside the context, this function returns the main queue if the call is made from the main thread . If the call is made from any other thread, this function returns the default parallel queue.

0
source share

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.

0
source share

New way for macOS: 10.12 and iOS: 10.0 (tvOS: 10.0, watchOS: 3.0) and higher.

ObjC: check if the main queue is working:

 if (dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(dispatch_get_main_queue())) { NSLog(@"Running on main queue"); } 

Confirm if running in the main queue:

 dispatch_assert_queue(dispatch_get_main_queue()); 

Confirm if it does not work in the main queue:

 dispatch_assert_queue_not(dispatch_get_main_queue()); 

Swift 3, 4 and later:

Check if it works in the main queue (unfortunately, there is no direct method):

 if (OperationQueue.current?.underlyingQueue?.label == DispatchQueue.main.label) { print("On main queue") } 

Confirm if running in the main queue:

 dispatchPrecondition(condition: .onQueue(.main)) // Code running on main queue 

Confirm if it does not work in the main queue:

 dispatchPrecondition(condition: .notOnQueue(.main)) // Code NOT running on main queue 

NOTE: mainThread != mainQueue The main queue is always executed on the main thread, but the queue can be executed on the main thread without being the main queue. So don't mix thread and queue tests!

0
source share

dispatch_get_current_queue deprecated since iOS 6.0. It seems that [NSThread isMainThread] is the only way to go.

-one
source share

All Articles