1.

场景需求:需要异步完成三个任务。任务一、任务二、任务三。要求:任务三必须在任务一、任务二完成之后触发。这就需要使用dispatch_barrier_async。



dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//任务1
for (int i = 0; i < 2; i++) {
NSLog(@"我是任务一、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务2
for (int i = 0; i < 2 ; i++) {
NSLog(@"我是任务二、来自线程:%@",[NSThread currentThread]);
}
});


dispatch_barrier_async(queue, ^{
//栅栏
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是分割线、来自线程:%@",[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
//任务3
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是任务三、来自线程:%@",[NSThread currentThread]);
}
});