Type of dispatch queues:
serial queue:
A task on the queue will be executed only after its previous queued task is returned, the tasks are executed based on first in first out order, so at any given time, only one task from the queue is executed. Note independent serial queues (which have different names) are processed concurrently with respect to each other, so serial only applies to the tasks within a specific serial queue.
concurrent queue:
the tasks on the queue are still executed in first in first out order, but the next tasks may be executed before the previous task returns, so there may be multiple tasks run at the same time.
Find which queue is serial or concurrent?
All global queues returned by dispatch_get_global_queue are concurrent queues
The main thread queue returned by dispathc_get_main_queue is a serial queue
Queues created by dispatch_queue_create may be serial or concurrent queue depending on the second parameter, if it is null or DISPATCH_QUEUE_SERIAL, then it is a serial queue. If the parameter is DISPATCH_QEUE_CONCURRENT, then it is a concurrent queue.
Ways to submit a queued task:
dispatch_async:
When the task is submitted, the call will return immediately, the queue will decide how to invoke it based on whether the current queue is concurrent or serial.
dispatch_sync
When the task is submitted, the call is blocked and cannot continue until the block is executed and returned.
Notice when submitting a block task to dispatch queue, the dispatch_block_t does not take any parameter or does not return any value. This should not be a big issue, as block can access the variables in caller's scope with enclosure.
Other notes:
1. Queue created by dispathc_queue_create method must be released by dispatch_queue_release method.
2. Any pending blocks submitted to a queue hold a reference to that queue, so the queue will not be released until all pending blocks have completed
serial queue:
A task on the queue will be executed only after its previous queued task is returned, the tasks are executed based on first in first out order, so at any given time, only one task from the queue is executed. Note independent serial queues (which have different names) are processed concurrently with respect to each other, so serial only applies to the tasks within a specific serial queue.
concurrent queue:
the tasks on the queue are still executed in first in first out order, but the next tasks may be executed before the previous task returns, so there may be multiple tasks run at the same time.
Find which queue is serial or concurrent?
All global queues returned by dispatch_get_global_queue are concurrent queues
The main thread queue returned by dispathc_get_main_queue is a serial queue
Queues created by dispatch_queue_create may be serial or concurrent queue depending on the second parameter, if it is null or DISPATCH_QUEUE_SERIAL, then it is a serial queue. If the parameter is DISPATCH_QEUE_CONCURRENT, then it is a concurrent queue.
Ways to submit a queued task:
dispatch_async:
When the task is submitted, the call will return immediately, the queue will decide how to invoke it based on whether the current queue is concurrent or serial.
dispatch_sync
When the task is submitted, the call is blocked and cannot continue until the block is executed and returned.
Notice when submitting a block task to dispatch queue, the dispatch_block_t does not take any parameter or does not return any value. This should not be a big issue, as block can access the variables in caller's scope with enclosure.
Other notes:
1. Queue created by dispathc_queue_create method must be released by dispatch_queue_release method.
2. Any pending blocks submitted to a queue hold a reference to that queue, so the queue will not be released until all pending blocks have completed
No comments:
Post a Comment