In Grand Central Dispatch, how does the dispatcher work with different queues (serial
and concurrent
) when using the dispatch_sync
function and the dispatch_async
function?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- How to send parameters to queues?
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
First of all we need two type of queue: one serial and one concurrent:
So, we can start with the first experiment, using a serial queue and all
dispatch_async
function to add our block to the queue:As you can see, the block are added to the queue but in the meantime the dispatcher begin to execute them. This is a characteristic of the
dispatcher_async
function, that is add block to the queue without wait that them finish to be executed. In other words, if you are usingdispatch_async
in a function, the function return immediately and in the meantime the block is executing. This is extremely useful! In this example i used NSLog to report when the block are added to the queue and so this does to be the execution slower and causes the logat the end. But as we will see after, without logs it will be wrote at the beginning. Due the fact that we are using a serial queue, the block are executed in the order of adding.
Next:
In this example we are using
dispatch_sync
function with a serial queue. It's very easy to see that all block are added when the previous block finish to be executed. This is a characteristic ofdispatch_sync
. In other words the function doesn't return until the block finish to be executed. Due the fact that is a serial queue, the order also here is respected.Next:
As i said before, here i show how work
dispatch_async
but with a concurrent queue. It's really interesting because without NSLog to show when the block is added, you can see how ALL BLOCKS are added before that the first block is executed. This behaviour is not constant. Can happen that block1 is executed and immediately after thedispatch_async
finish to add all blocks to the queue and then continue to execute other blocks. Another thing to note, is that the block are executed concurrently, and so them doesn't respect the order of adding and also this behaviour is not constant but depend from the CPU usage, performance and many other things.Next:
Finally, here we used
dispatch_sync
with concurrent queue. It seems perfectly equal to the couple sync/serial. In this case is so. This behaviour is due the fact that also if the queue is concurrent, it haven't got other block to execute because we are using the synchronous dispatcher and so, it wait to add the next block until the actual doesn't finish to be executed. This type of couple (sync/concurrent) is useful if we are also adding to the queue, blocks withdispatch_async
. In that case the dispatcher could have other blocks added to the queue to be executed.Hope that this mini demonstration has been useful ;)
Cheers!