Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is dispatch_sync(dispatch_get_global_queue(xxx), task) sync or async

As Apple's document says, dispatch_get_global_queue() is a concurrent queue, and dispatch_sync is something meaning serial.Then the tasks are processed async or sync?

like image 956
keywind Avatar asked Mar 22 '12 12:03

keywind


1 Answers

You're getting confused between what a queue is and what async vs sync means.

A queue is an entity on which blocks can be run. These can be serial or concurrent. Serial means that if you put block on in the order A, B, C, D, then they will be executed A, then B, then C, then D. Concurrent means that these same blocks might be executed in a different order and possibly even more than one at the same time (assuming you have multiple cores on which to run, obviously).

Then onto async vs sync. Async means that when you call dispatch_async, it will return immediately and the block will be queued on the queue. Sync means that when you call dispatch_sync it will return only after the block has finished executing.

So to fully answer your question, if you dispatch_sync onto a global concurrent queue then this block will be run, perhaps in parallel with other blocks on that queue, but in a synchronous manner - i.e. it won't return until the block is finished.

like image 182
mattjgalloway Avatar answered Nov 01 '22 01:11

mattjgalloway