282

I have a callback which might come from any thread. When I get this callback, then I would like to perform a certain task on the main thread.

Do I need to check whether I already am on the main thread - or is there any penalty by not performing this check before calling the code below?

dispatch_async(dispatch_get_main_queue(), ^{
   // do work here
});
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Egil
  • 4,265
  • 4
  • 20
  • 13

4 Answers4

164

No, you do not need to check whether you’re already on the main thread. By dispatching the block to the main queue, you’re just scheduling the block to be executed serially on the main thread, which happens when the corresponding run loop is run.

If you already are on the main thread, the behaviour is the same: the block is scheduled, and executed when the run loop of the main thread is run.

  • 3
    Question was whether there is a "penalty by not performing this check"... I would think that there's a performance penalty for using async dispatch when it's not necessary, or is it trivial? – Dan Rosenstark Dec 16 '11 at 19:33
  • 1
    @Yar I don’t think there’s a noticeable performance impact in most cases: GCD is a lightweight library. That said, I understood the question as: ‘given the code below, do I need to check whether I’m on the main thread?’ –  Dec 18 '11 at 10:17
  • 8
    You DO, however, need to check if you use dispatch_sync. Otherwise you get a deadlock. – Victor Engel Apr 18 '16 at 17:54
  • If you're on the main queue and dispatch `async` back onto the main queue, it will be run but that **may mess up the expected timing of your actions**. Such as UI code in `viewDidLoad()` [not running until *after* the view is first displayed](https://stackoverflow.com/questions/50727122/swift-threading-when-to-use-dispatchqueue-main-async/58703264). – pkamb Nov 05 '19 at 04:42
112

For the asynchronous dispatch case you describe above, you shouldn't need to check if you're on the main thread. As Bavarious indicates, this will simply be queued up to be run on the main thread.

However, if you attempt to do the above using a dispatch_sync() and your callback is on the main thread, your application will deadlock at that point. I describe this in my answer here, because this behavior surprised me when moving some code from -performSelectorOnMainThread:. As I mention there, I created a helper function:

void runOnMainQueueWithoutDeadlocking(void (^block)(void))
{
    if ([NSThread isMainThread])
    {
        block();
    }
    else
    {
        dispatch_sync(dispatch_get_main_queue(), block);
    }
}

which will run a block synchronously on the main thread if the method you're in isn't currently on the main thread, and just executes the block inline if it is. You can employ syntax like the following to use this:

runOnMainQueueWithoutDeadlocking(^{
    //Do stuff
});
Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • 2
    Why not call it something like "runOnMainQueueSync"? The fact that it could deadlock and does not is the kind of thing I wouldn't want to have all over my code. Thanks and +1 as always. – Dan Rosenstark Dec 18 '11 at 17:30
  • 2
    @Yar - I just gave it that name so that it was clear to me why I wasn't just firing off blocks synchronously on the main queue instead of using this helper function. It was more of a reminder to myself. – Brad Larson Dec 19 '11 at 18:18
  • 5
    It is still possible to deadlock with this function. Main-queue sync> other-queue sync> main-queue will deadlock. – hfossli Nov 06 '13 at 21:04
  • 3
    @hfossli - True, it won't handle every case. In my usage, I was always either calling in from an asynchronous dispatch on a background serial queue or inline code on the main thread. I wonder if `dispatch_set_specific()` would help in the case you describe: http://stackoverflow.com/a/12806754/19679 . – Brad Larson Nov 06 '13 at 22:19
  • 1
    [NSThread isMainThread] often returns YES and isn't considered safe for checking for this case in GCD programming. http://stackoverflow.com/questions/14716334/nsthread-ismainthread-always-returns-yes – Will Larche Sep 18 '14 at 20:39
  • @WillLarche - In this case, it is safe. The whole point of the above is to guarantee synchronous execution of a particular block on the main thread. If this function is run on the main thread, even if it is not on the main queue proper (the case you point to there), `-isMainThread` will return YES, and the block will run in-line as it should. It guarantees execution on the main thread, which preventing against the most direct deadlock cases (not all, as indicated above, but the ones I encounter in my code). – Brad Larson Sep 18 '14 at 20:47
69

As the other answers mentioned, dispatch_async from the main thread is fine.

However, depending on your use case, there is a side effect that you may consider a disadvantage: since the block is scheduled on a queue, it won't execute until control goes back to the run loop, which will have the effect of delaying your block's execution.

For example,

NSLog(@"before dispatch async");
dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"inside dispatch async block main thread from main thread");
});
NSLog(@"after dispatch async");

Will print out:

before dispatch async
after dispatch async
inside dispatch async block main thread from main thread

For this reason, if you were expecting the block to execute in-between the outer NSLog's, dispatch_async would not help you.

Michael Chinen
  • 17,737
  • 5
  • 33
  • 45
  • Needs to say that this is an important thing to consider – Marc-Alexandre Bérubé Jan 29 '16 at 13:45
  • Since you want to check, this means the code may or may not run in main thread. Already in main thread will run in that order, otherwise this can not be guaranteed. So you should **avoid** designing the code to run in specific order when refer to multithread programming. Try to use the async callback way. – ooops Dec 06 '16 at 16:49
1

No you don't need to check if you're in the main thread. Here is how you can do this in Swift:

runThisInMainThread { () -> Void in
    runThisInMainThread { () -> Void in
        // No problem
    }
}

func runThisInMainThread(block: dispatch_block_t) {
    dispatch_async(dispatch_get_main_queue(), block)
}

Its included as a standard function in my repo, check it out: https://github.com/goktugyil/EZSwiftExtensions

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168