5

I'm pretty clear what dispatch_async queue is performing, but I'm not clear what dispatch_sync purpose is. For an example: what is difference between this:

NSLog(@"A");
NSLog(@"B");

and this:

dispatch_sync(dispatch_get_main_queue(), ^ {
NSLog(@"A");
    });
NSLog(@"B");

As I get, in both ways output will be A then B. Because sync is executed in order that is written. Thanks.

Stefan
  • 1,283
  • 15
  • 33

4 Answers4

10

As the names says dispatch_sync makes it possible to synchronize the tasks to be executed even if they are not executed on the main queue.

Saheb Roy's answer is only half of the truth. You can only specify the dispatch queue your code should be executed on. The actual thread is chosen by GCD.

Code blocks dispatched using dispatch_async on a concurrent queue are also executed in the FIFO way and guaranteed to be executed in the order you dispatch them. The main difference is that dispatch_sync on a serial queue also guarantees you that following code blocks are not executed before the previous block has finished executing. dispatch_sync is blocking your current dispatch queue i.e. the queue your dispatch_sync call is executed on. So your calling function is blocked until the dispatched code block returns whereas dispatch_async returns immediately.

execution timeline using dispatch_async on a concurrent queue my look like this:

Block A   [..............]
Block B        [.....]
Block C           [....]

while using dispatch_sync on a serial queue looks like this:

Block A   [..............]
Block B                   [.....]
Block C                           [....]

RTasche
  • 2,614
  • 1
  • 15
  • 19
1

The purpose of dispatch_syncqueue is that it will dispatch blocks of code in the thread you mentioned and that the will run synchronously, meaning one by one or rather one after the other in FIFO method. Do check out NSOperationQueue to understand the function of dispatch_sync in a better way

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0

According to Docs

Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

Unlike with dispatch_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.

As an optimization, this function invokes the block on the current thread when possible.

Rajat
  • 10,977
  • 3
  • 38
  • 55
-5

Its purpose is to the multitasking. .Two or more process run at a same time one in background thread and another in main thread. .mostly the process run in background thread and UI update in main thread to avoid screen block.

baydi
  • 1,003
  • 6
  • 11
  • Well that's completely wrong maybe you might want to read http://stackoverflow.com/questions/21122842/whats-the-difference-between-synchronous-and-asynchronous-calls-in-objective-c – Popeye Sep 02 '15 at 09:41