Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatch_barrier_async equivalent in Swift 3

Tags:

Refactoring a colleague's code, and I'm looking for the equivalent of dispatch_barrier_async in swift 3. There are a lot of queues at play, and his design is to block only this queue, and only for this single operation.

// Swift 2.3 func subscribe(subscriber: DaoDelegate) {   dispatch_barrier_async(self.subscribers.q) { // NOTE: barrier, requires exclusive access for write     //...   } }  // Swift 3  func subscribe(subscriber: DaoDelegate) {   (self.subscribers.q).async { // (Not equivalent, no barrier on the concurrent queue)     //...   } } 

Can I keep that same functionality in Swift 3 without refactoring all the queue types?

like image 554
SimplGy Avatar asked Jun 28 '16 19:06

SimplGy


People also ask

What is Dispatch_barrier_async?

dispatch_barrier_sync(queue,void(^block)()) executes all the task blocks added before barrier in queue, then executes the block of barrier task, and then executes the task blocks added after barrier.

What is dispatch barrier Swift?

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.


1 Answers

The async() method has a flags parameter which accepts a .barrier option:

func subscribe(subscriber: DaoDelegate) {   (self.subscribers.q).async(flags: .barrier) {      //...   } } 
like image 189
Martin R Avatar answered Oct 28 '22 23:10

Martin R