美文网首页
GCD中的dispatch_barrier_async栅栏函数的

GCD中的dispatch_barrier_async栅栏函数的

作者: 景彧 | 来源:发表于2020-03-05 09:44 被阅读0次

【转载】原文链接

1.什么是dispatch_barrier_async函数
dispatch_barrier_async函数的作用与barrier的意思相同,在进程管理中起到一个栅栏的作用,它等待所有位于barrier函数之前的操作执行完毕后执行,并且在barrier函数执行之后,barrier函数之后的操作才会得到执行,该函数需要同dispatch_queue_create函数生成的concurrent Dispatch Queue队列一起使用

2.dispatch_barrier_async函数的作用

2.1 实现高效率的数据库访问和文件访问

2.2 避免数据竞争、保证数据安全

  1. dispatch_barrier_async实例
- (void)barrier {
  //同dispatch_queue_create函数生成的concurrent Dispatch Queue队列一起使用
    dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        NSLog(@"----1-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"----2-----%@", [NSThread currentThread]);
    });
    
    dispatch_barrier_async(queue, ^{
        NSLog(@"----barrier-----%@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"----3-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"----4-----%@", [NSThread currentThread]);
    });
}

出结果:1 2 --> barrier -->3 4 其中1、2 与 3、4 由于并行处理先后顺序不定

相关文章

网友评论

      本文标题:GCD中的dispatch_barrier_async栅栏函数的

      本文链接:https://www.haomeiwen.com/subject/knvblhtx.html