初識 GCD 中的 dispatch_barrier_async 函數(shù)

void
dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);

提交一個異步執(zhí)行的代碼塊到隊列中執(zhí)行

它有2個參數(shù):queue為dispatch_barrier_async 作用的隊列,block 為進入此隊列執(zhí)行的代碼塊

值得注意的是:dispatch_barrier_async 函數(shù)只有在 DISPATCH_QUEUE_CONCURRENT 隊列中才起作用,在全局并發(fā)隊列 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 中無效

dispatch_barrier_async 效果類似 dispatch_async,區(qū)別就是中間多了一個barrier,barrier顧名思義就是屏障的意思,將隊列一分為2,前面的代碼執(zhí)行完才能執(zhí)行dispatch_barrier_async中的任務(wù),最后執(zhí)行隊列后的任務(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容