dispatch_barrier_async和dispatch_barrier_sync

dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----- %@",[NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier函数l开始");
NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});

dispatch_async(queue, ^{
    NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");

执行结束
download1----- {number = 3, name = (null)}
download3----- {number = 5, name = (null)}
download2----- {number = 4, name = (null)}
barrier函数l开始
barrier函数----- {number = 4, name = (null)}
download4----- {number = 4, name = (null)}

dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{
    NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"download3----- %@",[NSThread currentThread]);
});

dispatch_barrier_sync(queue, ^{
    NSLog(@"barrier函数l开始");
    NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});

dispatch_async(queue, ^{
    NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");

download3----- {number = 5, name = (null)}
download1----- {number = 3, name = (null)}
download2----- {number = 4, name = (null)}
barrier函数l开始
barrier函数----- {number = 1, name = main}
执行结束
download4----- {number = 4, name = (null)}

dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{
    NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延时函数----- %@",[NSThread currentThread]);
    });
    NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"download3----- %@",[NSThread currentThread]);
});

dispatch_barrier_sync(queue, ^{
    NSLog(@"barrier函数l开始");
    NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});

dispatch_async(queue, ^{
    NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");

download1----- {number = 3, name = (null)}
download2----- {number = 4, name = (null)}
download3----- {number = 5, name = (null)}
barrier函数l开始
barrier函数----- {number = 1, name = main}
执行结束
download4----- {number = 5, name = (null)}
延时函数----- {number = 1, name = main}

dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{
    NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延时函数----- %@",[NSThread currentThread]);
    });
    NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
    NSLog(@"download3----- %@",[NSThread currentThread]);
});

dispatch_barrier_async(queue, ^{
    NSLog(@"barrier函数l开始");
    NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});

dispatch_async(queue, ^{
    NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");

执行结束
download1----- {number = 3, name = (null)}
download2----- {number = 3, name = (null)}
download3----- {number = 4, name = (null)}
barrier函数l开始
barrier函数----- {number = 4, name = (null)}
download4----- {number = 4, name = (null)}
延时函数----- {number = 1, name = main}

  1. dispatch_barrier_async 的作用是等待队列的前面的任务执行完毕后,才执行dispatch_barrier_async的block里面的任务,不会阻塞主线程

  2. dispatch_barrier_sync 的作用是等待队列的前面的任务执行完毕后,才执行dispatch_barrier_async的block里面的任务,阻塞主线程

  3. 当dispatch_barrier_async和dispatch_barrier_sync的队列里面有异步的任务的时候,dispatch_barrier不会等到异步的任务执行完成才执行。

你可能感兴趣的:(dispatch_barrier_async和dispatch_barrier_sync)