iOS开发学习iOS DeveloperiOS开发

iOS GCD栅栏函数

2017-05-30  本文已影响46人  BEYOND黄

避免数据竞争。

-(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]);

}

});

}

上一篇下一篇

猜你喜欢

热点阅读