在 Grand Central Dispatch 中使用串行队列的dispatch_async 与dispatch_sync

发布于 2024-10-17 13:43:52 字数 456 浏览 0 评论 0原文

好吧,我喜欢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2024-10-24 13:43:52

dispatch_async() 意味着该块已入队,并且 dispatch_async() 返回在执行该块之前将另一个任务/块(可能)入队。

使用dispatch_sync(),该块将被排队,并且该函数不会继续将另一个任务/块排队,直到该块被执行。

这些块仍然串行执行。您可以执行 100 个 dispatch_async() 调用,每个调用都有一个休眠 100 秒的块,而且速度非常快。接下来,在同一串行队列上调用 dispatch_sync()dispatch_sync() 将在大约 10,000 秒后返回。


更简单地说:

dispatch_async(serialQ, block1);
dispatch_async(serialQ, block2);
dispatch_sync(serialQ, block3);

block1 将在 block2 之前执行,而 block2 将在 block3 之前执行。这就是串行队列保证的顺序。

但是,对 dispatch_async() 的调用可能会在任何块开始执行之前返回。在执行所有三个块之前,dispatch_sync()不会返回!

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!

童话里做英雄 2024-10-24 13:43:52

dispatch_asyncdispatch_sync 都不会改变块排队的方式。如果队列是串行的,则块将以串行方式执行,如果队列是并发的,则以并发方式执行。

两者之间的重要区别在于,dispatch_sync 将块排队并等待当前执行线程,直到执行该块,而 dispatch_async 只是将块排队并继续执行后续指示。

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.

甜味拾荒者 2024-10-24 13:43:52

串行队列一次只能运行一个任务,无论同步还是异步。串行队列仅分配一个线程。使用下面的示例会更容易理解 -

假设有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文