I was going through DispatchQueue barrier and noticed both there are two ways async(flags: .barrier)
and sync(flags: .barrier)
I understood the use of the async barrier but confused with the sync barrier.
My confusion the task I want to do can be performed with this also
DispatchQueue.global().sync {
}
then what is the use of sync barrier? Why they are used? and how different in this.
DispatchQueue.global().sync(flags: .barrier) {
}
In general, dispatch queues will only perform one task at a time in a first-in, first-out kind of fashion. They can, however, be configured to schedule work concurrently. The main dispatch queue is a queue that runs one task at a time.
Overview. Use a barrier to synchronize the execution of one or more tasks in your dispatch queue. When you add a barrier to a concurrent dispatch queue, the queue delays the execution of the barrier block (and any tasks submitted after the barrier) until all previously submitted tasks finish executing.
Since its concurrent queue, tasks may not finish in the order they are added to queue. But with synchronous operation it does although they may be processed by different threads. So, it behaves as this is the serial queue. Remember using GCD you are only adding task to the Queue and performing task from that queue.
DispatchGroup allows for aggregate synchronization of work. It can be used to submit multiple different work items or blocks and track when they all complete, even though they might run on different queues.
There are two different things to consider here:
Sync/Asyc determins the program flow on the submitting queue: Using sync()
causes the execution on the submitting queue to block until the task has completed; in contrast, using async()
won't block.
Using the flag .barrier
, however, affects the way the blocks are executed on the queue they were submitted to (obviously, this only makes a difference on concurrent queues):
A block submitted with this flag will act as a barrier: all other blocks that were submitted before the barrier will finish and only then the barrier block will execute. All blocks submitted after the barrier will not start until the barrier has finished.
Note: the barrier flag will have no effect on global queues. You must create your own concurrent queue in order to be able to use barrier blocks. (Thanks to Rob for clearly pointing that out!)
At Lutz says (+1), sync
/async
and barriers are two completely different issues. The sync
/async
dictates the behavior of the calling thread (i.e. does it wait or not). Barriers dictate the behavior of the queue to which it it was dispatched (whether it’s allowed to run concurrently with any other dispatched blocks to that queue).
Note, though, that barriers do not work on global queues; they only affect private concurrent queues that you created. As the docs say about barriers:
The queue you specify should be a concurrent queue that you create yourself... If the queue you pass to this function is a serial queue or one of the global concurrent queues, this function behaves [as if it were dispatched without the barrier].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With