1、GCD知识讲解
1)dispatch_semaphore_create(long value) //创建一个信号量,总量为value,value不能小于0
2)dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout) //如果信号总量为0,进入等待状态,信号量大于0时,继续执行代码,同时将 信号量减1
3)dispatch_semaphore_signal(dispatch_semaphore_t desma) //发送信号量,信号量就会加1
2、例子
1 dispatch_semaphore_t lock = dispatch_semaphore_create(1); 2 3 for(int i=0;i<9;i++){ 4 dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); 5 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 6 NSLog(@"我是第%d进入",i); 7 dispatch_semaphore_signal(lock); 8 }); 9 }