避免數(shù)據(jù)競爭。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
{
//0.獲得全局并發(fā)隊列
//柵欄函數(shù)不能使用全局并發(fā)隊列
//dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_CONCURRENT);
//1.異步函數(shù)
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]);
}
});
//柵欄函數(shù)
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]);
}
});
}