dispatch_async()
means that the block is enqueued and dispatch_async()
returns to enqueueing another task/block (possibly) prior to the block being executed.
With dispatch_sync()
, the block is enqueued and the function will not continue enqueueing another task/block until the block is executed.
The blocks are still executed serially. You could execute 100 dispatch_async()
calls, each with a block that sleeps for 100 seconds, and it'd be really fast. Follow that with a call to dispatch_sync()
on the same serial queue and dispatch_sync()
will return ~10,000 seconds later.
To put it more simply:
dispatch_async(serialQ, block1);
dispatch_async(serialQ, block2);
dispatch_sync(serialQ, block3);
block1
will be executed before block2
which will be executed before block3
. That is the order guaranteed by the serial queue.
However, the calls to dispatch_async()
may return before any of the blocks start executing. The dispatch_sync()
will not return before all three blocks are executed!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…