陈斌彬的技术博客

Stay foolish,stay hungry

GCD-dispatch_barrier_async

先看段代码

    dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-1");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-2");
    });
    dispatch_barrier_async(concurrentQueue, ^(){
        NSLog(@"dispatch-barrier"); 
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-3");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-4");
    });

dispatch_barrier_async 作用是在并行队列中,等待前面两个操作并行操作完成,这里是并行输出

dispatch-1,dispatch-2

然后执行

dispatch_barrier_async中的操作,(现在就只会执行这一个操作)执行完成后,即输出
"dispatch-barrier,

最后该并行队列恢复原有执行状态,继续并行执行

dispatch-3,dispatch-4