在 Grand Central Dispatch 中使用串行队列的dispatch_async 与dispatch_sync
好吧,我喜欢 Grand Central Dispatch,并且在使用它后取得了相对成功,但这是我不完全理解的事情。
假设我使用 After 创建了自己的串行队列
dispatch_queue_t myQueue;
myQueue = dispatch_queue_create("myQueue", NULL);
,然后我执行以下操作:
dispatch_async(myQueue, ^{
[self doStuff1];
});
// and a few lines later...
dispatch_sync(myQueue, ^{
[self doStuff2];
});
第一个调度是异步的。那么,这将同时进行,对吗?如果 myQueue 是串行的,那怎么可能呢?串行队列如何并行或无序地执行操作?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
dispatch_async()
意味着该块已入队,并且dispatch_async()
返回在执行该块之前将另一个任务/块(可能)入队。使用
dispatch_sync()
,该块将被排队,并且该函数不会继续将另一个任务/块排队,直到该块被执行。这些块仍然串行执行。您可以执行 100 个
dispatch_async()
调用,每个调用都有一个休眠 100 秒的块,而且速度非常快。接下来,在同一串行队列上调用dispatch_sync()
,dispatch_sync()
将在大约 10,000 秒后返回。更简单地说:
block1
将在block2
之前执行,而block2
将在block3
之前执行。这就是串行队列保证的顺序。但是,对
dispatch_async()
的调用可能会在任何块开始执行之前返回。在执行所有三个块之前,dispatch_sync()
将不会返回!dispatch_async()
means that the block is enqueued anddispatch_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 todispatch_sync()
on the same serial queue anddispatch_sync()
will return ~10,000 seconds later.To put it more simply:
block1
will be executed beforeblock2
which will be executed beforeblock3
. That is the order guaranteed by the serial queue.However, the calls to
dispatch_async()
may return before any of the blocks start executing. Thedispatch_sync()
will not return before all three blocks are executed!dispatch_async
或dispatch_sync
都不会改变块排队的方式。如果队列是串行的,则块将以串行方式执行,如果队列是并发的,则以并发方式执行。两者之间的重要区别在于,dispatch_sync 将块排队并等待当前执行线程,直到执行该块,而 dispatch_async 只是将块排队并继续执行后续指示。
Neither
dispatch_async
ordispatch_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 anddispatch_async
just queues the block and continues the execution of the subsequent instructions.串行队列一次只能运行一个任务,无论同步还是异步。串行队列仅分配一个线程。使用下面的示例会更容易理解 -
假设有 2 个队列 A 和 B 分别运行任务 T1 和 T2,并且 T1 正在异步执行。如果控制权从 A 传递到 B 并且 B 同步运行 T2,那么在 T2(dispatch_sync 块中的代码块)完成执行之前,T1 将被阻塞。当 T2 完成时,T1 将恢复执行。
Serial Queue can only run a single task at a time,irrespective of sync or async. Serial queues are allocated only one thread. This will be easier to understand using the below example -
Suppose there are 2 Queues A and B running tasks T1 and T2 respectively and T1 is being executed asynchronously. If control passes from A to B and B runs T2 synchronously then till the time the T2(block of code in the dispatch_sync block) completes execution T1 will be blocked. When T2 completes then T1 will resume it's execution.