dispatch_barrier_async

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dispatch_barrier_async相关的知识,希望对你有一定的参考价值。

代码:

dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{
    NSLog(@"Before Barrier Block - 1");
});
dispatch_async(queue, ^{
    NSLog(@"Before Barrier Block - 2");
});
dispatch_async(queue, ^{
    NSLog(@"Before Barrier Block - 3");
});
dispatch_async(queue, ^{
    NSLog(@"Before Barrier Block - 4");
});
dispatch_async(queue, ^{
    NSLog(@"Before Barrier Block - 5");
});

dispatch_barrier_async(queue, ^{
    NSLog(@"Barrier Block");
});

dispatch_async(queue, ^{
    NSLog(@"After Barrier Block - 1");
});
dispatch_async(queue, ^{
    NSLog(@"After Barrier Block - 2");
});
dispatch_async(queue, ^{
    NSLog(@"After Barrier Block - 3");
});
dispatch_async(queue, ^{
    NSLog(@"After Barrier Block - 4");
});
dispatch_async(queue, ^{
    NSLog(@"After Barrier Block - 5");
}); 

可能的输出:

Before Barrier Block - 1
Before Barrier Block - 2
Before Barrier Block - 3
Before Barrier Block - 5
Before Barrier Block - 4
Barrier Block
After Barrier Block - 1
After Barrier Block - 3
After Barrier Block - 2
After Barrier Block - 5
After Barrier Block - 4

备注:

dispatch_barrier_async用于在同一个并发队列中,同步多个并发操作

以上是关于dispatch_barrier_async的主要内容,如果未能解决你的问题,请参考以下文章

dispatch_barrier_async 似乎对全局队列没有影响?

GCD中的dispatch_barrier_async函数的使用(栅栏函数)

iOS 读写操作 处理 pthread_rwlock dispatch_barrier_async

Android Adapter代码片

代码片|水波纹

iOS 多线程:『GCD』详尽总结