• 栅栏函数dispatch_barrier_sync/async


    有几个并行执行的操作:1,2,3,4,5

    如果我们想要在1,2,3执行完毕后,执行A操作,然后再执行4,5 那么我们就可以使用栅栏函数来解决;

    1、使用dispatch_barrier_sync 串行 函数:
    在插入队列的时候是串行的,在任务真正执行的过程中也是串行的。

    - (void)testCustomBarrier{
        dispatch_queue_t currentQueue = dispatch_queue_create("testCustomBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 111");
        });
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 222");
        });
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 333");
        });
    
        dispatch_barrier_sync(currentQueue, ^{
            NSLog(@"============= AAA barrier sync operation");
        });
        NSLog(@"============= After AAA barrier operation");
    
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 444");
        });
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 555");
        });
    }

    打印结果:

    2019-07-02 09:38:20.744906+0800 testImportFiles[68888:20594679] ============= 111
    2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594680] ============= 333
    2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594681] ============= 222
    2019-07-02 09:38:20.745081+0800 testImportFiles[68888:20594637] ============= AAA barrier sync operation
    2019-07-02 09:38:20.745091+0800 testImportFiles[68888:20594637] ============= After AAA barrier operation
    2019-07-02 09:38:20.745168+0800 testImportFiles[68888:20594680] ============= 444
    2019-07-02 09:38:20.745174+0800 testImportFiles[68888:20594681] ============= 555

    2、使用dispatch_barrier_async 异步 函数:
    在插入队列的时候是异步的,在任务真正执行的过程中是串行的。

    - (void)testCustomBarrier{
        dispatch_queue_t currentQueue = dispatch_queue_create("testCustomBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 111");
        });
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 222");
        });
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 333");
        });
    
        dispatch_barrier_async(currentQueue, ^{
            NSLog(@"============= AAA barrier async operation");
        });
        NSLog(@"============= After AAA barrier operation");
    
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 444");
        });
        dispatch_async(currentQueue, ^{
            NSLog(@"============= 555");
        });
    }

    打印结果和 1 一样:

    2019-07-02 09:38:20.744906+0800 testImportFiles[68888:20594679] ============= 111
    2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594680] ============= 333
    2019-07-02 09:38:20.744913+0800 testImportFiles[68888:20594681] ============= 222
    2019-07-02 09:38:20.745071+0800 testImportFiles[68888:20594637] ============= After AAA barrier operation
    2019-07-02 09:38:20.745081+0800 testImportFiles[68888:20594637] ============= AAA barrier async operation
    2019-07-02 09:38:20.745168+0800 testImportFiles[68888:20594680] ============= 444
    2019-07-02 09:38:20.745174+0800 testImportFiles[68888:20594681] ============= 555

    实际上dispatch_barrier_sync与dispatch_barrier_async 都会等待在它前面插入队列的任务(这里就是1,2,3)先执行完。且都会等待他们自己的任务(barrier operation)执行完之后再执行后面的任务(4,5)。

    而dispatch_barrier_sync与dispatch_barrier_async的不同点:
    dispatch_barrier_sync需要等待自己的任务(barrier operation)执行完毕后,才会插入后面的任务(4,5),然后执行后面的任务。
    dispatch_barrier_async将自己的任务插入到queue之后,不会等待自己的任务(barrier operation)执行结果,它会继续插入后面的任务。

    转自:https://blog.csdn.net/xuanweihong_ios/article/details/94430867

  • 相关阅读:
    javascript运行机制之执行顺序详解
    js常见错误类型
    原生JS添加类名 删除类名
    innerHTML、innerText和outerHTML、outerText的区别
    cmd应用基础教程
    ASIC中的一些库和文件类型
    VCS中的覆盖率分析
    NC_Verilog中的工具ICC
    UVM中的sequence使用(一)
    UVM中的regmodel建模(三)
  • 原文地址:https://www.cnblogs.com/huangzs/p/13518016.html
Copyright © 2020-2023  润新知