keywind
keywind

Reputation: 1195

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?

Upvotes: 5

Views: 3943

Answers (2)

mattjgalloway
mattjgalloway

Reputation: 34912

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.

Upvotes: 33

hp iOS Coder
hp iOS Coder

Reputation: 3234

Apple Doc says

dispatch_get_global_queue

Returns a well-known global concurrent queue of a given priority level.

dispatch_queue_t dispatch_get_global_queue( long priority, unsigned long flags);

Parameters

priority The priority of the queue being retrieved. For a list of possible values, see “dispatch_queue_priority_t”. flags This value is reserved for future use. You should always pass 0. Return Value Returns the requested global queue.

Discussion

The well-known global concurrent queues cannot be modified. Calls to dispatch_suspend, dispatch_resume, dispatch_set_context, and the like have no effect when used with queues returned by this function.

Blocks submitted to these global concurrent queues may be executed concurrently with respect to each other.

Availability Available in iOS 4.0 and later. Declared In dispatch/queue.h

In the Discussion they have said that-> 'blocks submitted MAY be executed concurrently wrt each other.'

So Tasks may be processed Async with each other.

Upvotes: 0

Related Questions