iOS中的GCD(二)

dispatch_barrier_async

测试代码:

- (void)test_barrier_async_concurrent_queue {
    
    dispatch_queue_t concurrent_queue = dispatch_queue_create("com.jueyingxx.concurrent_queue", DISPATCH_QUEUE_CONCURRENT);
    
    for (NSInteger i = 0; i < 10; i++) {
        dispatch_async(concurrent_queue, ^{
            NSLog(@"index == %@", @(i));
        });
    }
    
    dispatch_barrier_async(concurrent_queue, ^{
        NSLog(@"barrier is run, %@", [NSThread currentThread]);
    });
    
    NSLog(@"main thread = %@", [NSThread currentThread]);
    
    for (NSInteger i = 10; i < 20; i++) {
        dispatch_async(concurrent_queue, ^{
            NSLog(@"index == %@", @(i));
        });
    }
}

输出结果:

2016-10-25 17:58:16.608 GCD_Demo[14950:9777240] index == 6
2016-10-25 17:58:16.608 GCD_Demo[14950:9777239] index == 5
2016-10-25 17:58:16.608 GCD_Demo[14950:9777221] index == 1
2016-10-25 17:58:16.608 GCD_Demo[14950:9777238] index == 4
2016-10-25 17:58:16.608 GCD_Demo[14950:9777220] index == 0
2016-10-25 17:58:16.608 GCD_Demo[14950:9777241] index == 7
2016-10-25 17:58:16.608 GCD_Demo[14950:9777182] main thread = <NSThread: 0x61000007d640>{number = 1, name = main}
2016-10-25 17:58:16.609 GCD_Demo[14950:9777242] index == 8
2016-10-25 17:58:16.608 GCD_Demo[14950:9777223] index == 2
2016-10-25 17:58:16.608 GCD_Demo[14950:9777237] index == 3
2016-10-25 17:58:16.609 GCD_Demo[14950:9777243] index == 9
2016-10-25 17:58:16.610 GCD_Demo[14950:9777243] barrier is run, <NSThread: 0x608000272cc0>{number = 4, name = (null)}
2016-10-25 17:58:16.610 GCD_Demo[14950:9777237] index == 10
2016-10-25 17:58:16.610 GCD_Demo[14950:9777223] index == 11
2016-10-25 17:58:16.610 GCD_Demo[14950:9777242] index == 12
2016-10-25 17:58:16.610 GCD_Demo[14950:9777241] index == 13
2016-10-25 17:58:16.610 GCD_Demo[14950:9777220] index == 14
2016-10-25 17:58:16.610 GCD_Demo[14950:9777243] index == 16
2016-10-25 17:58:16.610 GCD_Demo[14950:9777221] index == 17
2016-10-25 17:58:16.610 GCD_Demo[14950:9777239] index == 18
2016-10-25 17:58:16.610 GCD_Demo[14950:9777240] index == 19
2016-10-25 17:58:16.611 GCD_Demo[14950:9777238] index == 15

结论:

1、dispatch_async_barrier和dispatch_sync_barrier的唯一区别就是async,会创建新的线程。
2、dispatch_async_barrier,我们可以使用它做这样的需求,就是在同一个并发队列中,我们期望一部分任务先完成,然后再进行后面的任务,或者说后面要执行的任务依赖前面的任务完成。符合这样的情景的我们就应该使用。
3、在串行队列中使用dispatch_async_barrier没有任何意义,因为,在串行队列中的任务本来就是FIFO的。
4、在全局队列中使用dispatch_async_barrier也是没有任何意义的,因为系统每次创建的全局队列有可能都不是同一个队列,所以使用dispatch_async_barrier没有意义。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • GCD笔记 总结一下多线程部分,最强大的无疑是GCD,那么先从这一块部分讲起. Dispatch Queue的种类...
    jins_1990阅读 4,121评论 0 1
  • 本篇博客共分以下几个模块来介绍GCD的相关内容: 多线程相关概念 多线程编程技术的优缺点比较? GCD中的三种队列...
    dullgrass阅读 37,946评论 28 236
  • iOS中GCD的使用小结 作者dullgrass 2015.11.20 09:41*字数 4996阅读 20199...
    DanDanC阅读 4,505评论 0 0
  • 背景 担心了两周的我终于轮到去医院做胃镜检查了!去的时候我都想好了最坏的可能(胃癌),之前在网上查的症状都很相似。...
    Dely阅读 13,030评论 21 42
  • 醒来, 若还是一片狼藉 那就对了 说明世界不会为了你改变 终于 还是被世俗的电话催醒 走出寒舍 可爱的小女孩 默默...
    涩先森阅读 1,547评论 0 6