dispatch_barrier_async dispatch_barrier_sync
串行队列 与 同步栅栏 dispatch_barrier_sync
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
NSLog(@"当前线程%@",[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]);
});
当前线程<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)}
可以看出 串行队列 同步栅栏 是在主线程完成的 栅栏执行完 "我累了我要休息一会" 后执行下面的函数
并发队列 与 同步栅栏 dispatch_barrier_sync
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"当前线程%@",[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]);
});
当前线程<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)}
效果同上 同步并发队列 一样在主线程执行 等待5秒 执行下面的函数
串行队列 与 异步栅栏 dispatch_barrier_async
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
NSLog(@"当前线程%@",[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]);
});
当前线程<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)}
可以看出串行异步栅栏 先执行栅栏里面的函数 后执行下面的函数
并发队列 与 异步栅栏 dispatch_barrier_async
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"当前线程%@",[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]);
});
当前线程<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)}
效果同上
可以看出 栅栏函数内部执行的任务如果 不开启子线程 都是先执行栅栏里面的函数 在执行下面的函数
如果栅栏函数里面添加异步执行会有什么效果?
串行队列与同步栅栏
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
NSLog(@"当前线程%@",[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]);
});
当前线程<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)}
可以看出 虽然异步子线程执行任务 但是同步栅栏一样要等到 栅栏里的任务执行完毕才会 执行下面的函数
串行队列与异步栅栏
dispatch_queue_t quene = dispatch_queue_create("yc", DISPATCH_QUEUE_SERIAL);
NSLog(@"当前线程%@",[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]);
});
当前线程<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)}
我累了我要休息一会
可以看出 异步栅栏 先执行下面的任务 后完成栅栏里面子线程操作的任务
栅栏内部函数如果在子线程中执行 同步栅栏会等到任务完成 在向下执行 异步栅栏不会拦截 继续执行下面的任务。
网友评论