void
dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
提交一個(gè)異步執(zhí)行的代碼塊到隊(duì)列中執(zhí)行
它有2個(gè)參數(shù):queue為dispatch_barrier_async 作用的隊(duì)列,block 為進(jìn)入此隊(duì)列執(zhí)行的代碼塊
值得注意的是:dispatch_barrier_async 函數(shù)只有在 DISPATCH_QUEUE_CONCURRENT 隊(duì)列中才起作用,在全局并發(fā)隊(duì)列 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 中無效
dispatch_barrier_async 效果類似 dispatch_async需五,區(qū)別就是中間多了一個(gè)barrier绽快,barrier顧名思義就是屏障的意思啸盏,將隊(duì)列一分為2激挪,前面的代碼執(zhí)行完才能執(zhí)行dispatch_barrier_async中的任務(wù),最后執(zhí)行隊(duì)列后的任務(wù)
例如
dispatch_queue_t concurrent_queue = dispatch_queue_create("concurrent_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrent_queue, ^(){
NSLog(@"task-1--%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^(){
NSLog(@"task-2--%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^(){
NSLog(@"task-3--%@",[NSThread currentThread]);
});
dispatch_barrier_sync(concurrent_queue, ^(){
NSLog(@"dispatch_barrier_async--%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^(){
NSLog(@"task-4--%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^(){
NSLog(@"task-5--%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^(){
NSLog(@"task-6--%@",[NSThread currentThread]);
});
使用dispatch_barrier_async
2016-12-26 22:29:13.983 GCD[1443:100483] task-1--<NSThread: 0x7f8cc1d755d0>{number = 4, name = (null)}
2016-12-26 22:29:13.983 GCD[1443:100491] task-3--<NSThread: 0x7f8cc1e07960>{number = 3, name = (null)}
2016-12-26 22:29:13.983 GCD[1443:100472] task-2--<NSThread: 0x7f8cc1f01450>{number = 2, name = (null)}
2016-12-26 22:29:13.984 GCD[1443:100472] dispatch_barrier_async--<NSThread: 0x7f8cc1f01450>{number = 2, name = (null)}
2016-12-26 22:29:13.984 GCD[1443:100472] task-4--<NSThread: 0x7f8cc1f01450>{number = 2, name = (null)}
2016-12-26 22:29:13.984 GCD[1443:100483] task-6--<NSThread: 0x7f8cc1d755d0>{number = 4, name = (null)}
2016-12-26 22:29:13.984 GCD[1443:100491] task-5--<NSThread: 0x7f8cc1e07960>{number = 3, name = (null)}
使用dispatch_barrier_sync
2016-12-26 22:20:27.318 GCD[1420:95930] task-2--<NSThread: 0x7fad53d3faf0>{number = 3, name = (null)}
2016-12-26 22:20:27.318 GCD[1420:95919] task-1--<NSThread: 0x7fad53c1c360>{number = 2, name = (null)}
2016-12-26 22:20:27.318 GCD[1420:95936] task-3--<NSThread: 0x7fad53f01c90>{number = 4, name = (null)}
2016-12-26 22:20:27.319 GCD[1420:95836] dispatch_barrier_sync--<NSThread: 0x7fad53d07de0>{number = 1, name = main}
2016-12-26 22:20:27.320 GCD[1420:95936] task-4--<NSThread: 0x7fad53f01c90>{number = 4, name = (null)}
2016-12-26 22:20:27.320 GCD[1420:95930] task-6--<NSThread: 0x7fad53d3faf0>{number = 3, name = (null)}
2016-12-26 22:20:27.320 GCD[1420:95919] task-5--<NSThread: 0x7fad53c1c360>{number = 2, name = (null)}
task-1/2/3 和 task-4/5/6 分別并發(fā)執(zhí)行笔宿,dispatch_barrier_async就像一座屏障淑履,把1/2/3和4/5/6分隔開來,
dispatch_barrier_sync 與 dispatch_barrier_async 的區(qū)別則是同步和異步的區(qū)別,可以參照 dispatch_sync 和 dispatch_async