美文网首页
dispatch_barrier_sync 和dispatch_

dispatch_barrier_sync 和dispatch_

作者: 姜小舟 | 来源:发表于2020-05-18 16:28 被阅读0次

    先看官方文档

    • dispatch_barrier_sync
      Submits a barrier block object for execution and waits until that block completes.(提交一个栅栏函数在执行中,它会等待栅栏函数执行完)
    • dispatch_barrier_async
      Submits a barrier block for asynchronous execution and returns immediately.(提交一个栅栏函数在异步执行中,它会立马返回)
    • The queue you specify should be a concurrent queue that you create yourself using the dispatch_queue_create function. If the queue you pass to this function is a serial queue or one of the global concurrent queues, this function behaves like the dispatch_sync function.

    即:

    • dispatch_barrier_sync 需要等待栅栏执行完才会执行栅栏后面的任务
    • dispatch_barrier_async 无需等待栅栏执行完,会继续往下走(保留在队列里)
      在同步栅栏时栅栏函数在主线程中执行,而异步栅栏中开辟了子线程栅栏函数在子线程中执行
    • 在使用栅栏函数时.使用自定义队列才有意义,如果用的是串行队列或者系统提供的全局并发队列,这个栅栏函数的作用等同于一个同步函数的作用

    结合示例来看一下:

    需求: 有2个任务{1, 2},执行完前2个再执行后2个{3, 4}

    这里我们用到栅栏函数dispatch_barrier_(a)sync,(也可以用队列组),我们要注意的是不能使用全局并发队列(系统提供给我们的)否则会散失栅栏函数的意义。

    • dispatch_barrier_sync

    - (void)test {
        //创建一个自定义并发队列
        NSLog(@"------start------")
    
        dispatch_queue_t queue = dispatch_queue_create("...", DISPATCH_QUEUE_CONCURRENT);
        //一步函数,无序
        dispatch_async(queue,  ^{
            NSLog(@"------1------");
        });
        dispatch_async(queue,  ^{
            NSLog(@"------2------");
        });
    
        dispatch_barrier_sync(queue,  ^{
            NSLog(@"++++ dispatch_barrier_sync +++++");
        });
    
        NSLog(@"------middle------");
    
        dispatch_async(queue,  ^{
            NSLog(@"------3------");
        });
        dispatch_async(queue,  ^{
            NSLog(@"%@------4------");
        });
    
        NSLog(@"%@------end------");
    }
    #打印:
    ------start------
    ------1------
    ------2------
    ++++ dispatch_barrier_sync +++++
    ------middle------
    ------end------
    ------3------
    ------4------
    #再次打印:
    ------start------
    ------2------
    ------1------
    ++++ dispatch_barrier_sync +++++
    ------middle------
    ------end------
    ------3------
    ------4------
    #结论:栅栏函数确实分割了任务,但是任务执行的顺序的确是无序的(异步函数)
    
    • dispatch_barrier_async

    - (void)test {
        //创建一个自定义并发队列
        NSLog(@"------start------")
    
        dispatch_queue_t queue = dispatch_queue_create("...", DISPATCH_QUEUE_CONCURRENT);
        //一步函数,无序
        dispatch_async(queue,  ^{
            NSLog(@"------1------");
        });
        dispatch_async(queue,  ^{
            NSLog(@"------2------");
        });
    
        dispatch_barrier_async(queue,  ^{
            NSLog(@"++++ dispatch_barrier_async +++++");
        });
    
        NSLog(@"------middle------");
    
        dispatch_async(queue,  ^{
            NSLog(@"------3------");
        });
        dispatch_async(queue,  ^{
            NSLog(@"%@------4------");
        });
    
        NSLog(@"%@------end------");
    }
    #打印:
    ------start------
    ------middle------
    ------1------
    ------2------
    ------end------
    ++++ dispatch_barrier_async +++++
    ------3------
    ------4------
    #再次打印:
    ------start------
    ------middle------
    ------1------
    ------end------
    ------2------
    ++++ dispatch_barrier_async +++++
    ------3------
    ------4------
    #结论:栅栏函数确实分割了任务,但是任务执行的顺序的确是无序的(异步函数)
    

    相关文章

      网友评论

          本文标题:dispatch_barrier_sync 和dispatch_

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