dispatch_barrier_async在指定的队列中添加一个barrier任务,当在该任务添加之前的任务全部完成后,执行barrier对应的任务,然后执行后续添加的并行任务。无意中发现,如果使用系统自带的global_queue并不能完成理想的效果,可能是评估考虑到该队列会有一些系统相关的任务,将自带的并行队列设置成了不可添加barrier。如下代码,使用不同的queue完全是不同的效果
- (void)func06
{
// dispatch_queue_t queue = dispatch_queue_create("MYQUEUE", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 4; i++) {
NSLog(@"1.1: == %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 5; i++) {
NSLog(@"1.2: == %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
}
});
NSLog(@"开始");
dispatch_barrier_async(queue, ^{
NSLog(@"BARRIER");
});
NSLog(@"退出");
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"2: == %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
}
});
}