有几个并行执行的操作:1,2,3,4,5
如果我们想要在1,2,3执行完毕后,执行A操作,然后再执行4,5 那么我们就可以使用栅栏函数来解决;
1、使用dispatch_barrier_sync 串行 函数:
在插入队列的时候是串行的,在任务真正执行的过程中也是串行的。
- (void)testCustomBarrier{
dispatch_queue_t currentQueue = dispatch_queue_create("testCustomBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(currentQueue, ^{
NSLog(@"============= 111");
});
dispatch_async(currentQueue, ^{
NSLog(@"============= 222");
});
dispatch_async(currentQueue, ^{
NSLog(@"============= 333");
});
dispatch_barrier_sync(currentQueue, ^{
NSLog(@"============= AAA barrier sync operation");
});
NSLog(@"============= After AAA barrier operation");
dispatch_async(currentQueue, ^{
NSLog(@"============= 444");
});
dispatch_async(currentQueue, ^{
NSLog(@"============= 555");
});
}
打印结果:
2019-07-02 09:38:20.744906+0800 testImportFiles[68888:20594679] ============= 111
2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594680] ============= 333
2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594681] ============= 222
2019-07-02 09:38:20.745081+0800 testImportFiles[68888:20594637] ============= AAA barrier sync operation
2019-07-02 09:38:20.745091+0800 testImportFiles[68888:20594637] ============= After AAA barrier operation
2019-07-02 09:38:20.745168+0800 testImportFiles[68888:20594680] ============= 444
2019-07-02 09:38:20.745174+0800 testImportFiles[68888:20594681] ============= 555
2、使用dispatch_barrier_async 异步 函数:
在插入队列的时候是异步的,在任务真正执行的过程中是串行的。
- (void)testCustomBarrier{
dispatch_queue_t currentQueue = dispatch_queue_create("testCustomBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(currentQueue, ^{
NSLog(@"============= 111");
});
dispatch_async(currentQueue, ^{
NSLog(@"============= 222");
});
dispatch_async(currentQueue, ^{
NSLog(@"============= 333");
});
dispatch_barrier_async(currentQueue, ^{
NSLog(@"============= AAA barrier async operation");
});
NSLog(@"============= After AAA barrier operation");
dispatch_async(currentQueue, ^{
NSLog(@"============= 444");
});
dispatch_async(currentQueue, ^{
NSLog(@"============= 555");
});
}
打印结果和 1 一样:
2019-07-02 09:38:20.744906+0800 testImportFiles[68888:20594679] ============= 111
2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594680] ============= 333
2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594681] ============= 222
2019-07-02 09:38:20.745071+0800 testImportFiles[68888:20594637] ============= After AAA barrier operation
2019-07-02 09:38:20.745081+0800 testImportFiles[68888:20594637] ============= AAA barrier async operation
2019-07-02 09:38:20.745168+0800 testImportFiles[68888:20594680] ============= 444
2019-07-02 09:38:20.745174+0800 testImportFiles[68888:20594681] ============= 555
实际上dispatch_barrier_sync与dispatch_barrier_async 都会等待在它前面插入队列的任务(这里就是1,2,3)先执行完。且都会等待他们自己的任务(barrier operation)执行完之后再执行后面的任务(4,5)。
而dispatch_barrier_sync与dispatch_barrier_async的不同点:
dispatch_barrier_sync需要等待自己的任务(barrier operation)执行完毕后,才会插入后面的任务(4,5),然后执行后面的任务。
dispatch_barrier_async将自己的任务插入到queue之后,不会等待自己的任务(barrier operation)执行结果,它会继续插入后面的任务。
转自:https://blog.csdn.net/xuanweihong_ios/article/details/94430867