Duck
Duck

Reputation: 35953

Is the main Grand Central Dispatch queue serial or concurrent?

Suppose I call dispatch_async() three times in order:

dispatch_async(dispatch_get_main_queue(),
         ^{
             [self doOne];
});

// some code here

dispatch_async(dispatch_get_main_queue(),
         ^{
             [self doTwo];
});

// more code here

dispatch_async(dispatch_get_main_queue(),
         ^{
             [self doThree];
});

Will this always be executed like

[self doOne], [self doTwo], then [self doThree], or is the order is guaranteed?

In this case, the question probably is if the main queue is serial or concurrent.

Upvotes: 7

Views: 2461

Answers (2)

jscs
jscs

Reputation: 64002

Concurrency Programming Guide, About Dispatch Queues:

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

Upvotes: 4

Martin R
Martin R

Reputation: 539775

From the documentation:

dispatch_get_main_queue

Returns the serial dispatch queue associated with the application’s main thread.

so the main queue is a serial queue, and [self doOne], [self doTwo], [self doThree] are executed sequentially in that order.

Upvotes: 17

Related Questions