Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCD Queue Types

I'm trying to understand the differences between the types of queues. As I understand it there are 3 types:

  • Global queue - concurrent - blocks are executed as soon as possible regardless of order
  • Main queue - serial - blocks are executed as they were submitted
  • Private queue - serial

What I'm wondering is: What are the differences between dispatch_sync and dispatch_async when submitted to each kind of queue? This is how I understand it thus far:

dispatch_sync(global_queue)^
{
     // blocks are executed one after the other in no particular order
     // example: block 3 executes. when it finishes block 7 executes.
}

dispatch_async(global_queue)^
{
     // blocks are executed concurrently in no particular order
     // example: blocks 2,4,5,7 execute at the same time.
}

dispatch_sync(main_queue)^
{
     // blocks are executed one after the other in the order they were submitted
     // example: block 1 executes. when it finishes block 2 will execute and so forth.
}

dispatch_async(main_queue)^
{
     // blocks are executed concurrently in the order they were submitted
     // example: blocks 1-4 (or whatever amount of threads the system can handle at one time) will fire at the same time. 
     // when the first block completes block 5 will then execute.
}

I'd like to know how much my perception of this is correct.

like image 387
Brian Semiglia Avatar asked Mar 26 '26 01:03

Brian Semiglia


1 Answers

the difference between dispatch_sync and dispatch_async is kind like sendmessage and postmessage in windows API. dispatch_sync Submits a block object for execution on a dispatch queue and waits until that block completes. dispatch_async Submits a block for asynchronous execution on a dispatch queue and returns immediately.

The target queue determines whether the block is invoked serially or concurrently with respect to other blocks submitted to that same queue. Independent serial queues are processed concurrently with respect to each other.

you should really read the document first carefully before post the question.

like image 61
Toni lee Avatar answered Apr 02 '26 12:04

Toni lee