GCD中的并发与串行队列 [英] Concurrent vs serial queues in GCD

查看:142
本文介绍了GCD中的并发与串行队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难全面了解GCD中的并发和串行队列。我有一些问题,希望有人可以清楚地回答我,在这一点上。


  1. 并用于一个接一个执行任务。但是,如果:




    • 我创建了一个序列队列

    • 我使用 dispatch_async (在刚创建的串行队列中)三次以分派三个块A,B,C



    将按照A,B,C的顺序执行三个块:





    • 同时(在同一时间对分区线程),因为我使用ASYNC派遣


  2. 我读到我可以在并发队列中使用 dispatch_sync 之后。在这种情况下,WHY做串行队列甚至存在,因为我总是可以使用一个并发队列,我可以分发SYNCHRONOUSLY与我想要的块一样多。



    感谢任何好解释!



解决方案

一个简单的例子:分钟执行。将它添加到主线程的队列中。让我们看看这四种情况。




  • async - concurrent:代码在后台线程上运行。控制立即返回到主线程(和UI)。块不能假定它是队列上运行的唯一块。

  • async - serial:代码在后台线程上运行。控制立即返回到主线程。 可以假设它是该队列上运行的唯一块

  • sync - concurrent:代码在后台线程上运行,但主线程等待完成,阻止对UI的任何更新。该块不能假定它是在该队列上运行的唯一块(我可以使用异步几秒钟之前添加另一个块)

  • sync - serial:代码在背景上运行线程,但主线程等待它完成,阻止对UI的任何更新。 可以假设它是该队列上唯一的块



显然,的最后两个用于长时间运行的进程。你通常看到它,当你试图更新UI(总是在主线程)从可能在另一个线程上运行的东西。


I'm struggling to fully understand the concurrent and serial queues in GCD. I have some issues and hoping someone can answer me clearly and at the point.

  1. I'm reading that serial queues are created and used in order to execute tasks one after the other. However, what happens if:

    • I create a serial queue
    • I use dispatch_async (on the serial queue I just created) three times to dispatch three blocks A,B,C

    Will the three blocks be executed:

    • in order A,B,C because the queue is serial

      OR

    • concurrently (in the same time on parralel threads) because I used ASYNC dispatch
  2. I'm reading that I can use dispatch_sync on concurrent queues in order to execute blocks one after the other. In that case, WHY do serial queues even exist, since I can always use a concurrent queue where I can dispatch SYNCHRONOUSLY as many blocks as I want?

    Thanks for any good explanation!

解决方案

A simple example: you have a block that takes a minute to execute. You add it to a queue from the main thread. Let's look at the four cases.

  • async - concurrent: the code runs on a background thread. Control returns immediately to the main thread (and UI). The block can't assume that it's the only block running on that queue
  • async - serial: the code runs on a background thread. Control returns immediately to the main thread. The block can assume that it's the only block running on that queue
  • sync - concurrent: the code runs on a background thread but the main thread waits for it to finish, blocking any updates to the UI. The block can't assume that it's the only block running on that queue (I could have added another block using async a few seconds previously)
  • sync - serial: the code runs on a background thread but the main thread waits for it to finish, blocking any updates to the UI. The block can assume that it's the only block running on that queue

Obviously you wouldn't use either of the last two for long running processes. You normally see it when you're trying to update the UI (always on the main thread) from something that may be running on another thread.

这篇关于GCD中的并发与串行队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆