dispatch_async vs. dispatch_sync using Serial Queues in Grand Central Dispatch

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

OK, I love Grand Central Dispatch and after using it with relative success but this is something I don't fully understand.

Suppose I have created my own serial queue using

dispatch_queue_t myQueue; myQueue = dispatch_queue_create("myQueue", NULL); 

After that I do this:

dispatch_async(myQueue, ^{   [self doStuff1]; });  // and a few lines later...  dispatch_sync(myQueue, ^{   [self doStuff2]; }); 

The first dispatch is async. So, it will be done concurrently, right? How can that be if myQueue is serial? How can a serial queue do things in parallel or, if you will, out of order?

thanks

回答1:

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!



回答2:

Neither dispatch_async or dispatch_sync change the way the block gets queued. If the queue is serial, the blocks will execute in a serial manner, if the queue is concurrent, in a concurrent manner.

The important difference between the two is that dispatch_sync queues the block and waits on the current execution thread until that block is executed and dispatch_async just queues the block and continues the execution of the subsequent instructions.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!