GCD柵欄函數(shù)

dispatch_barrier_async dispatch_barrier_sync

串行隊列 與 同步柵欄 dispatch_barrier_sync
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
    NSLog(@"當(dāng)前線程%@",[NSThread currentThread]);
    dispatch_async(quene, ^{
        NSLog(@"1號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_barrier_sync(quene, ^{
        NSLog(@"柵欄操作線程------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"我累了我要休息一會");
    });
    
    dispatch_async(quene, ^{
        
        NSLog(@"2號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_async(quene, ^{
        NSLog(@"3號我在哪里啊-----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(quene, ^{
        NSLog(@"4號我在哪里啊-----%@",[NSThread currentThread]);
    });
當(dāng)前線程<NSThread: 0x6000026a1f80>{number = 1, name = main}
1號我在哪里啊-----<NSThread: 0x6000026ca700>{number = 3, name = (null)}
柵欄操作線程------<NSThread: 0x6000026a1f80>{number = 1, name = main}
我累了我要休息一會
2號我在哪里啊-----<NSThread: 0x6000026ca700>{number = 3, name = (null)}
3號我在哪里啊-----<NSThread: 0x6000026ca700>{number = 3, name = (null)}
4號我在哪里啊-----<NSThread: 0x6000026ca700>{number = 3, name = (null)}

可以看出 串行隊列 同步柵欄 是在主線程完成的 柵欄執(zhí)行完 "我累了我要休息一會" 后執(zhí)行下面的函數(shù)

并發(fā)隊列 與 同步柵欄 dispatch_barrier_sync
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"當(dāng)前線程%@",[NSThread currentThread]);
    
    dispatch_async(quene, ^{
        NSLog(@"1號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_barrier_sync(quene, ^{
        NSLog(@"柵欄操作線程1------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"我累了我要休息一會");
    });
    
    dispatch_async(quene, ^{
        
        NSLog(@"2號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_async(quene, ^{
        NSLog(@"3號我在哪里啊-----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(quene, ^{
        NSLog(@"4號我在哪里啊-----%@",[NSThread currentThread]);
    });
當(dāng)前線程<NSThread: 0x600001a12480>{number = 1, name = main}
1號我在哪里啊-----<NSThread: 0x600001a7f380>{number = 3, name = (null)}
柵欄操作線程------<NSThread: 0x600000796fc0>{number = 1, name = main}
我累了我要休息一會
2號我在哪里啊-----<NSThread: 0x600001a7f380>{number = 3, name = (null)}
3號我在哪里啊-----<NSThread: 0x600001a78dc0>{number = 4, name = (null)}
4號我在哪里啊-----<NSThread: 0x600001a79040>{number = 5, name = (null)}

效果同上 同步并發(fā)隊列 一樣在主線程執(zhí)行 等待5秒 執(zhí)行下面的函數(shù)

串行隊列 與 異步柵欄 dispatch_barrier_async
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
    NSLog(@"當(dāng)前線程%@",[NSThread currentThread]);
    
    dispatch_async(quene, ^{
        NSLog(@"1號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_barrier_async(quene, ^{
        NSLog(@"柵欄操作線程1------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"我累了我要休息一會");
    });
    
    dispatch_async(quene, ^{
        
        NSLog(@"2號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_async(quene, ^{
        NSLog(@"3號我在哪里啊-----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(quene, ^{
        NSLog(@"4號我在哪里啊-----%@",[NSThread currentThread]);
    });
當(dāng)前線程<NSThread: 0x60000045d400>{number = 1, name = main}
1號我在哪里啊-----<NSThread: 0x60000043a7c0>{number = 3, name = (null)}
柵欄操作線程1------<NSThread: 0x60000043a7c0>{number = 3, name = (null)}
我累了我要休息一會
2號我在哪里啊-----<NSThread: 0x60000043a7c0>{number = 3, name = (null)}
3號我在哪里啊-----<NSThread: 0x60000043a7c0>{number = 3, name = (null)}
4號我在哪里啊-----<NSThread: 0x60000043a7c0>{number = 3, name = (null)}

可以看出串行異步柵欄 先執(zhí)行柵欄里面的函數(shù) 后執(zhí)行下面的函數(shù)

并發(fā)隊列 與 異步柵欄 dispatch_barrier_async
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"當(dāng)前線程%@",[NSThread currentThread]);
    
    dispatch_async(quene, ^{
        NSLog(@"1號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_barrier_async(quene, ^{
        NSLog(@"柵欄操作線程------%@",[NSThread currentThread]);
        sleep(5);
        NSLog(@"我累了我要休息一會");
    });
    
    dispatch_async(quene, ^{
        
        NSLog(@"2號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_async(quene, ^{
        NSLog(@"3號我在哪里啊-----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(quene, ^{
        NSLog(@"4號我在哪里啊-----%@",[NSThread currentThread]);
    });
當(dāng)前線程<NSThread: 0x6000009f1400>{number = 1, name = main}
1號我在哪里啊-----<NSThread: 0x60000099a240>{number = 3, name = (null)}
柵欄操作線程------<NSThread: 0x60000099a240>{number = 3, name = (null)}
我累了我要休息一會
2號我在哪里啊-----<NSThread: 0x60000099a240>{number = 3, name = (null)}
3號我在哪里啊-----<NSThread: 0x600000986dc0>{number = 4, name = (null)}
4號我在哪里啊-----<NSThread: 0x60000099aec0>{number = 5, name = (null)}

效果同上

可以看出 柵欄函數(shù)內(nèi)部執(zhí)行的任務(wù)如果 不開啟子線程 都是先執(zhí)行柵欄里面的函數(shù) 在執(zhí)行下面的函數(shù)

如果柵欄函數(shù)里面添加異步執(zhí)行會有什么效果?

串行隊列與同步柵欄

dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
    NSLog(@"當(dāng)前線程%@",[NSThread currentThread]);
    
    dispatch_async(quene, ^{
        NSLog(@"1號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_barrier_sync(quene, ^{
        NSLog(@"柵欄操作線程1------%@",[NSThread currentThread]);
        dispatch_async(quene, ^{
            NSLog(@"柵欄操作線程2------%@",[NSThread currentThread]);
            sleep(5);
            NSLog(@"我累了我要休息一會");
        });
    });
    
    dispatch_async(quene, ^{
        
        NSLog(@"2號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_async(quene, ^{
        NSLog(@"3號我在哪里啊-----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(quene, ^{
        NSLog(@"4號我在哪里啊-----%@",[NSThread currentThread]);
    });
當(dāng)前線程<NSThread: 0x600002c55380>{number = 1, name = main}
1號我在哪里啊-----<NSThread: 0x600002c306c0>{number = 3, name = (null)}
柵欄操作線程1------<NSThread: 0x600002c55380>{number = 1, name = main}
柵欄操作線程2------<NSThread: 0x600002c306c0>{number = 3, name = (null)}
我累了我要休息一會
 2號我在哪里啊-----<NSThread: 0x600002c306c0>{number = 3, name = (null)}
3號我在哪里啊-----<NSThread: 0x600002c306c0>{number = 3, name = (null)}
4號我在哪里啊-----<NSThread: 0x600002c306c0>{number = 3, name = (null)}

可以看出 雖然異步子線程執(zhí)行任務(wù) 但是同步柵欄一樣要等到 柵欄里的任務(wù)執(zhí)行完畢才會 執(zhí)行下面的函數(shù)

串行隊列與異步柵欄

dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
    NSLog(@"當(dāng)前線程%@",[NSThread currentThread]);
    
    dispatch_async(quene, ^{
        NSLog(@"1號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_barrier_async(quene, ^{
        NSLog(@"柵欄操作線程1------%@",[NSThread currentThread]);
        dispatch_async(quene, ^{
            NSLog(@"柵欄操作線程2------%@",[NSThread currentThread]);
            sleep(5);
            NSLog(@"我累了我要休息一會");
        });
    });
    
    dispatch_async(quene, ^{
        
        NSLog(@"2號我在哪里啊-----%@",[NSThread currentThread]);
    });
    
    dispatch_async(quene, ^{
        NSLog(@"3號我在哪里啊-----%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(quene, ^{
        NSLog(@"4號我在哪里啊-----%@",[NSThread currentThread]);
    });
當(dāng)前線程<NSThread: 0x600003388440>{number = 1, name = main}
1號我在哪里啊-----<NSThread: 0x6000033e8bc0>{number = 3, name = (null)}
柵欄操作線程1------<NSThread: 0x6000033e8bc0>{number = 3, name = (null)}
2號我在哪里啊-----<NSThread: 0x6000033e8bc0>{number = 3, name = (null)}
3號我在哪里啊-----<NSThread: 0x6000033e8bc0>{number = 3, name = (null)}
4號我在哪里啊-----<NSThread: 0x6000033e8bc0>{number = 3, name = (null)}
柵欄操作線程2------<NSThread: 0x6000033e8bc0>{number = 3, name = (null)}
我累了我要休息一會

可以看出 異步柵欄 先執(zhí)行下面的任務(wù) 后完成柵欄里面子線程操作的任務(wù)

柵欄內(nèi)部函數(shù)如果在子線程中執(zhí)行 同步柵欄會等到任務(wù)完成 在向下執(zhí)行 異步柵欄不會攔截 繼續(xù)執(zhí)行下面的任務(wù)。

?著作權(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)容