iOS GCD栅栏函数
避免数据竞争。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
//0.获得全局并发队列
//栅栏函数不能使用全局并发队列
//dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_CONCURRENT);
//1.异步函数
dispatch_async(queue, ^{
for(NSIntegeri =0; i<100; i++) {
NSLog(@"download1-%zd-%@",i,[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for(NSIntegeri =0; i<100; i++) {
NSLog(@"download2-%zd-%@",i,[NSThreadcurrentThread]);
}
});
//栅栏函数
dispatch_barrier_async(queue, ^{
NSLog(@"+++++++++++++++++++++++++++++");
});
dispatch_async(queue, ^{
for(NSIntegeri =0; i<100; i++) {
NSLog(@"download3-%zd-%@",i,[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for(NSIntegeri =0; i<100; i++) {
NSLog(@"download4-%zd-%@",i,[NSThreadcurrentThread]);
}
});
}