dispatch_barrier_async和dispatch_barrier_sync的区别和详细解析

96
刀客传奇
0.6 2018.02.23 23:39 字数 517

版本记录

版本号 时间
V1.0 2018.02.23

前言

dispatch_barrier_asyncdispatch_barrier_sync大家在做执行任务的时候经常用到的函数,这一篇就主要分析一下这两个函数的用法和区别。

dispatch_barrier_sync

先看一下这个函数的API

/*!
 * @function dispatch_barrier_sync
 *
 * @abstract
 * Submits a barrier block for synchronous execution on a dispatch queue.
   为队列上的同步执行提交阻塞的代码块。
 *
 * @discussion
 * Submits a block to a dispatch queue like dispatch_sync(), but marks that
 * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
   将块提交到dispatch_sync()等调度队列,但将该块标记为阻塞(仅与DISPATCH_QUEUE_CONCURRENT队列相关)。
 *
 * See dispatch_sync() for details.
 *
 * @param queue
 * The target dispatch queue to which the block is submitted.
 * The result of passing NULL in this parameter is undefined.
   block要提交的目标队列,传递NULL参数的结果未定义。
 *
 * @param block
 * The block to be invoked on the target dispatch queue.
 * The result of passing NULL in this parameter is undefined.
 */
#ifdef __BLOCKS__
API_AVAILABLE(macos(10.7), ios(4.3))
DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
void
dispatch_barrier_sync(dispatch_queue_t queue,
        DISPATCH_NOESCAPE dispatch_block_t block);
#endif

下面大家看一下简单的示例:

- (void)initSyncBarrier
{
    //1 创建并发队列
    dispatch_queue_t concurrentQueue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    
    //2 向队列中添加任务
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 1,%@",[NSThread currentThread]);
    });
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 2,%@",[NSThread currentThread]);
    });
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 3,%@",[NSThread currentThread]);
    });
    dispatch_barrier_sync(concurrentQueue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"barrier");
    });
    NSLog(@"aa, %@", [NSThread currentThread]);
    
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 4,%@",[NSThread currentThread]);
    });
    NSLog(@"bb, %@", [NSThread currentThread]);
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 5,%@",[NSThread currentThread]);
    });
}

看一下输出结果

2018-02-23 23:16:46.987147+0800 JJWebImage[1399:39117] Task 2,<NSThread: 0x6040002653c0>{number = 4, name = (null)}
2018-02-23 23:16:46.987145+0800 JJWebImage[1399:39125] Task 3,<NSThread: 0x604000265200>{number = 5, name = (null)}
2018-02-23 23:16:46.987154+0800 JJWebImage[1399:39116] Task 1,<NSThread: 0x60000027cc40>{number = 3, name = (null)}
2018-02-23 23:16:47.987597+0800 JJWebImage[1399:39014] barrier
2018-02-23 23:16:47.987860+0800 JJWebImage[1399:39014] aa, <NSThread: 0x60000006c900>{number = 1, name = main}
2018-02-23 23:16:47.988025+0800 JJWebImage[1399:39014] bb, <NSThread: 0x60000006c900>{number = 1, name = main}
2018-02-23 23:16:47.988055+0800 JJWebImage[1399:39116] Task 4,<NSThread: 0x60000027cc40>{number = 3, name = (null)}
2018-02-23 23:16:47.988263+0800 JJWebImage[1399:39118] Task 5,<NSThread: 0x6040002652c0>{number = 6, name = (null)}

我们可以看到:

  • Task1,2,3不是顺序执行的因为是异步,但是都在barrier的前面,Task4,5在barrier的后面执行。
  • aa和bb都在主线程进行输出。
  • 执行完barrier,才会将后面的任务4,5插入到队列执行。

dispatch_barrier_async

首先看一下这个函数的API

/*!
 * @function dispatch_barrier_async
 *
 * @abstract
 * Submits a barrier block for asynchronous execution on a dispatch queue.
   为队列上的异步执行提交阻塞的代码块。
 *
 * @discussion
 * Submits a block to a dispatch queue like dispatch_async(), but marks that
 * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
  提交block块到dispatch_async()队列,但是标记该代码块为阻塞(仅与DISPATCH_QUEUE_CONCURRENT队列相关)。
 *
 * See dispatch_async() for details.
 *
 * @param queue
 * The target dispatch queue to which the block is submitted.
 * The system will hold a reference on the target queue until the block
 * has finished.
 * The result of passing NULL in this parameter is undefined.
  block要提交到的目标队列,系统会保持对目标队列的引用,直到block完成。传递NULL参数的结果未定义。
 *
 * @param block
 * The block to submit to the target dispatch queue. This function performs
 * Block_copy() and Block_release() on behalf of callers.
 * The result of passing NULL in this parameter is undefined.
 要提交到的目标队列的block, 这个函数代表调用者执行Block_copy()和Block_release()。传递NULL参数的结果未定义。
 */
#ifdef __BLOCKS__
API_AVAILABLE(macos(10.7), ios(4.3))
DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
void
dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
#endif

下面看一个简单的实例

- (void)initAsyncBarrier
{
    //1 创建并发队列
    dispatch_queue_t concurrentQueue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    
    //2 向队列中添加任务
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 1,%@",[NSThread currentThread]);
    });
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 2,%@",[NSThread currentThread]);
    });
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 3,%@",[NSThread currentThread]);
    });
    dispatch_barrier_async(concurrentQueue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"barrier");
    });
    NSLog(@"aa, %@", [NSThread currentThread]);
    
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 4,%@",[NSThread currentThread]);
    });
    NSLog(@"bb, %@", [NSThread currentThread]);
    dispatch_async(concurrentQueue, ^{
        NSLog(@"Task 5,%@",[NSThread currentThread]);
    });
}

看一下输出结果

2018-02-23 23:10:43.338610+0800 JJWebImage[1362:36007] aa, <NSThread: 0x604000062480>{number = 1, name = main}
2018-02-23 23:10:43.338665+0800 JJWebImage[1362:36154] Task 1,<NSThread: 0x604000275100>{number = 3, name = (null)}
2018-02-23 23:10:43.338664+0800 JJWebImage[1362:36153] Task 3,<NSThread: 0x60000007e640>{number = 4, name = (null)}
2018-02-23 23:10:43.339671+0800 JJWebImage[1362:36007] bb, <NSThread: 0x604000062480>{number = 1, name = main}
2018-02-23 23:10:43.338731+0800 JJWebImage[1362:36152] Task 2,<NSThread: 0x604000275040>{number = 5, name = (null)}
2018-02-23 23:10:44.341556+0800 JJWebImage[1362:36153] barrier
2018-02-23 23:10:44.341849+0800 JJWebImage[1362:36152] Task 5,<NSThread: 0x604000275040>{number = 5, name = (null)}
2018-02-23 23:10:44.341855+0800 JJWebImage[1362:36153] Task 4,<NSThread: 0x60000007e640>{number = 4, name = (null)}

大家可以看到:

  • Task1,2,3不是顺序执行的因为是异步,但是都在barrier的前面,Task4,5在barrier的后面执行。
  • aa和bb都在主线程进行输出。
  • 不用执行完barrier,就可以将任务4,5插入到队列中,但是仍然需要执行完barrier,才会执行任务4和5。

总结

你也可以这么理解,它们二者的差别在于插入barrier后面任务的时机不同。后面任务执行顺序都要在barrier之后,这一点是相同的。

1. 相同点

  • 等待在它前面插入队列的任务先执行完

  • 等待他们自己的任务执行完再执行后面的任务

2. 不同点

  • dispatch_barrier_sync将自己的任务插入到队列的时候,需要等待自己的任务结束之后才会继续插入被写在它后面的任务,然后执行它们。

  • dispatch_barrier_async将自己的任务插入到队列之后,不会等待自己的任务结束,它会继续把后面的任务插入到队列,然后等待自己的任务结束后才执行后面任务。

后记

本篇已结束,后面更精彩~~~

OC