GCD可以设置线程最大并发量

-(void)dispatchSignal{

     //crate的value表示,最多几个资源可访问
     dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);   
     dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     
     //任务1
     dispatch_async(quene, ^{
         dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
         NSLog (@ "run task 1" );
         sleep(1);
         NSLog (@ "complete task 1" );
         dispatch_semaphore_signal(semaphore);       
     });
     //任务2
     dispatch_async(quene, ^{
         dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
         NSLog (@ "run task 2" );
         sleep(1);
         NSLog (@ "complete task 2" );
         dispatch_semaphore_signal(semaphore);       
     });
     //任务3
     dispatch_async(quene, ^{
         dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
         NSLog (@ "run task 3" );
         sleep(1);
         NSLog (@ "complete task 3" );
         dispatch_semaphore_signal(semaphore);       
     });   
}

转载于:https://www.cnblogs.com/dashengios/p/10694945.html

你可能感兴趣的:(GCD可以设置线程最大并发量)