- dispatch_barrier_sync和dispatch_b
- dispatch_barrier_sync和dispatch_b
- dispatch_barrier_sync和dispatch_b
- dispatch_barrier_sync和dispatch_b
- dispatch_barrier_sync和dispatch_b
- dispatch_barrier_sync和dispatch_b
- dispatch_barrier_sync、dispatch_b
- dispatch_barrier_sync dispatch_b
- dispatch_barrier_sync与dispatch_b
- dispatch_barrier_sync与dispatch_b
写在最前面:
在使用栅栏函数时.使用自定义队列才有意义,如果用的是串行队列或者系统提供的全局并发队列,这个栅栏函数的作用等同于一个同步函数的作用
下面是测试执行代码,看过代码应该就知道区别了.
dispatch_queue_t queue = dispatch_queue_create("com.shen.thread.demo", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"start");
dispatch_async(queue, ^{
NSLog(@"currentThread-1:%@", [NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"currentThread-2:%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
});
NSLog(@"pause");
dispatch_async(queue, ^{
NSLog(@"currentThread-3:%@", [NSThread currentThread]);
});
NSLog(@"end");
这是异步的执行顺序
dispatch_queue_t queue = dispatch_queue_create("com.shen.thread.demo", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"start");
dispatch_async(queue, ^{
NSLog(@"currentThread-1:%@", [NSThread currentThread]);
});
dispatch_barrier_sync(queue, ^{
NSLog(@"currentThread-2:%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
});
NSLog(@"pause");
dispatch_async(queue, ^{
NSLog(@"currentThread-3:%@", [NSThread currentThread]);
});
NSLog(@"end");
网友评论