【问题标题】:dispatch_barrier_async seems no effect on the global queue?dispatch_barrier_async 似乎对全局队列没有影响?
【发布时间】:2016-07-31 10:38:56
【问题描述】:

当我尝试 GCD 函数 dispatch_barrier_async 时,它在 dispatch_queue_create 创建的队列上按预期工作,而当我将它放在 dispatch_get_global_queue 创建的全局队列上时,屏障似乎不再起作用 = = ,有人可以解释一下吗?谢谢~ the demo image

【问题讨论】:

    标签: ios ios-multithreading


    【解决方案1】:

    这并不奇怪,这是记录在案的行为。

    如果您使用它向您自己创建的队列添加一个块,那么它将阻止所有其他块,直到它完成。如果将其添加到公共队列,则其行为就像 dispatch_async

    https://developer.apple.com/reference/dispatch/1452797-dispatch_barrier_async的文档

    其中规定:

    您指定的队列应该是您使用 dispatch_queue_create 函数自己创建的并发队列。如果您传递给此函数的队列是串行队列或全局并发队列之一,则此函数的行为类似于 dispatch_async 函数。

    【讨论】:

    • 是的,我明白了,非常感谢你这么快又好,我没有仔细阅读API = =。
    【解决方案2】:

    全局队列是根队列,由线程池管理,是所有自定义队列的父队列。这意味着根队列管理的屏障规则是针对自定义队列的,而不是针对全局队列本身的。

    GCD exposes five different queues: the main queue running on the main thread,
    three background queues with different priorities, and one background queue 
    with an even lower priority, which is I/O throttled. Furthermore, you can 
    create custom queues, which can either be serial or concurrent queues. While 
    custom queues are a powerful abstraction, all blocks you schedule on them 
    will ultimately trickle down to one of the system’s global queues and its 
    thread pool(s).
    

    https://www.objc.io/issues/2-concurrency/concurrency-apis-and-pitfalls/

    【讨论】:

      猜你喜欢
      • 2015-10-31
      • 2019-02-16
      • 1970-01-01
      • 2021-12-24
      • 2012-10-22
      • 2021-11-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多