17

Does the following code run on the main thread? Does "main queue" refer to the main thread?

dispatch_async(dispatch_get_main_queue(),
^{
   // Some code
});
Jeremy
  • 1
  • 85
  • 340
  • 366
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Note that if you happen to do that and **YOU ARE ALREADY** on the main thread, in fact it is **QUITE HARMLESS**. This is very convenient when you have complex networking code! :) – Fattie Sep 29 '14 at 08:19

3 Answers3

28

The async part of dispatch async vs sync is different than concurrent vs serial. Async means that the function returns immediately, sync means that it'll wait until the block is executed. Since the main thread/queue is serial, things are going to get executed in order - I believe this means that since you're asking it to async dispatch on the same thread you're dispatching from, it'll return immediately, wait till the end of the current run loop and anything else in the queue, and then execute your block.

This is more useful for inside a queue than it is on the main thread - you can process your data, let the UI know to update, and continue processing without waiting for everything to redraw, etc. That's why you'll often see a dispatch_async call to the main thread inside another dispatch_async(concurrent queue) instead of just a dispatch_sync.

Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
24

Yes. From Apple developer site:

The dispatch framework provides a default serial queue for the application to use. This queue is accessed via dispatch_get_main_queue().

MByD
  • 135,866
  • 28
  • 264
  • 277
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
  • 2
    It is guaranteed to run on the main thread only if there is a run loop associated to the main thread. – CouchDeveloper Jul 12 '14 at 13:53
  • 1
    Here and below, CouchDeveloper is correct. Run `man dispatch_main`, and you'll find the following: "Cocoa applications need not call dispatch_main(). Blocks submitted to the main queue will be executed as part of the "common modes" of the application's main NSRunLoop or CFRunLoop. However, blocks submitted to the main queue in applications using dispatch_main() are not guaranteed to execute on the main thread." – beOn Apr 27 '18 at 06:00
7

This is documented in multiple places, including the docs for dispatch_get_main_queue() itself. The Concurrency Programming Guide says:

The main dispatch queue is a globally available serial queue that executes tasks on the application’s main thread.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 3
    Not quite correct: It is guaranteed to run on the main thread only if there is a run loop associated to the main thread. Otherwise, the block dispatched via `dispatch_async(dispatch_get_main_queue(), block)` may execute on any thread. – CouchDeveloper Jul 12 '14 at 13:55
  • 1
    The main thread always has a run loop in a Cocoa app, @CouchDeveloper. – jscs Jul 12 '14 at 18:07
  • @JoshCaswell The OP does´t mention Cocoa ;) – CouchDeveloper Jul 13 '14 at 13:40
  • In fact, on rereading, @CouchDeveloper, the doc I've quoted _does_ guarantee that, even if there's no run loop and you're not using Cocoa, the tasks will be performed on a main thread: https://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW15 If you have information to the contrary, you should post it in an answer. I for one will be interested to read it. – jscs Jul 13 '14 at 19:03
  • 2
    I stumbled over it by accident: in a simple console C++ program, using `dispatch_main()`: `dispatch_async(dispatch_get_main_queue(), ...)`did not execute in the main thread (consistently). Then, using a RunLoop, it executed on the main thread. The man page for dispatch does also not explicitly mention that it would execute on the main thread: "The dispatch framework provides a default serial queue for the application to use. This queue is accessed via the dispatch_get_main_queue() function." – CouchDeveloper Jul 14 '14 at 06:38
  • Ancient history now, but if you are still looking for information to the contrary `man dispatch_main` says " However, blocks submitted to the main queue in applications using dispatch_main() are not guaranteed to execute on the main thread." – dmaclach Dec 09 '22 at 03:12