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ù)。