美文网首页
GCD之dispatch_barrier_async(栅栏函数)

GCD之dispatch_barrier_async(栅栏函数)

作者: 红色海_ | 来源:发表于2019-12-02 17:10 被阅读0次
    dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
      dispatch_async(concurrentQueue, ^(){
        sleep(3);
        NSLog(@"dispatch-1");
      });
      dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-2");
      });
      dispatch_barrier_async(concurrentQueue, ^(){
        NSLog(@"dispatch-barrier");
      });
      dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-3");
      });
      dispatch_async(concurrentQueue, ^(){
        NSLog(@"dispatch-4");
      });
    

    dispatch_barrier_async 作用是在并行队列中,等待前面两个操作并行操作完成,这里是并行输出
    dispatch-1,dispatch-2
    然后执行
    dispatch_barrier_async中的操作,(现在就只会执行这一个操作)执行完成后,即输出
    "dispatch-barrier,
    最后该并行队列恢复原有执行状态,继续并行执行
    dispatch-3,dispatch-4

    相关文章

      网友评论

          本文标题:GCD之dispatch_barrier_async(栅栏函数)

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