版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.02.23 |
前言
dispatch_barrier_async
和dispatch_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
將自己的任務插入到隊列之后,不會等待自己的任務結束,它會繼續把后面的任務插入到隊列,然后等待自己的任務結束后才執行后面任務。
后記
本篇已結束,后面更精彩~~~