2

when I am try the GCD function dispatch_barrier_async, it worked as expected on queue created by dispatch_queue_create, while when I put it on the global queue created by dispatch_get_global_queue, the barrier seems not work any more = =, somebody can explain? thanks~ the demo image

MichaelMao
  • 528
  • 6
  • 15

2 Answers2

4

This isn't surprising, it's documented behaviour.

If you use this to add a block to a queue you create yourself, then it will block all other blocks until it completes. If you add it to a public queue, then it behaves just like dispatch_async

Documentation at https://developer.apple.com/reference/dispatch/1452797-dispatch_barrier_async

Which states:

The queue you specify should be a concurrent queue that you create yourself using the dispatch_queue_create function. If the queue you pass to this function is a serial queue or one of the global concurrent queues, this function behaves like the dispatch_async function.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Yeah, I see it, thank you very much for reply so quick and nice, I did not read the API very carefully = =. – MichaelMao Jul 31 '16 at 11:08
0

The global queue is the root queue, which is managed by the thread pool, and is all the custom queue's parent queue. That means the barrier rule managed by the root queue is for custom queues, not for the global queue itself.

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/

mlibai
  • 31
  • 5