当我们在处理多线程的时候,如果想控制并发线程的数量,我们会使用NSOperationQueue的maxConcurrentOperationCount来进行控制,所以遇到此类问题,我们一般会使用NSOperation+ NSOperationQueue来解决。
我们也可以使用GCD来解决这个问题,就是配合dispatch_semaphore来使用。 dispatch_semaphore就是信号量,在以前的Linux开发中就已经用过。信号量是一个整形值,在初始化的时候分配一个初始值,支持两个操作信号通知和等待。
等待
// 返回0:表示正常。返回非0:表示等待时间超时
long dispatch_semaphore_wait(dispatch_semaphore_t dsema,
dispatch_time_t timeout);
信号通知
long dispatch_semaphore_signal(dispatch_semaphore_t dsema);
创建信号量
// 值得注意的是,这里的传入的参数value必须大于或等于0,
//否则dispatch_semaphore_create会返回NULL
dispatch_semaphore_t dispatch_semaphore_create(long value);
当一个信号量被通知dispatch_semaphore_signal,计数会加1; 如果一个线程等待一个信号量dispatch_semaphore_wait,线程会被阻塞,直到计数器>0,此时开始运行,并且对信号量减1。
这样我们就可以根据 初始值 ,来控制可以有多少个并发的线程在运行。关于信号量,可以用停车位来比喻,如果停车场有5个停车位,都停满了,如果此时来了第6辆车,就需要等待,信号量的值就相当于剩余的车位的数量。dispatch_semaphore_wait函数就相当于来了一辆车,dispatch_semaphore_signal就相当于走了一辆车。
dispatch_semaphore_wait中的参数timeout表示超时时间,如果等待期间没有获取到信号量或者信号量的值一直为0,那么等到timeout时,其所处线程自动执行其后语句。可取值为:DISPATCH_TIME_NOW和 DISPATCH_TIME_FOREVER,我们也可以自己设置一个dispatch_time_t的时间值,表示超时时间为这个时间之后。
- DISPATCH_TIME_NOW:超时时间为0,表示忽略信号量,直接运行
- DISPATCH_TIME_FOREVER:超时时间为永远,表示会一直等待信号量为正数,才会继续运行 具体的例子:
- (void)testSemaphore{
dispatch_semaphore_t _semaphore = dispatch_semaphore_create(1);
NSLog(@"=== start _semaphore");
for (NSInteger i = 0; i < 10; i++) {
//异步
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// wait
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
//sleep
sleep(3);
NSLog(@"=== for 1 ---%zd %@", i, [NSThread currentThread]);
//signal
dispatch_semaphore_signal(_semaphore);
});
}
NSLog(@"=== mid ---_semaphore ");
for (NSInteger i = 0; i < 10; i++) {
//异步
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// wait
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
//sleep
sleep(3);
NSLog(@"=== for 2 ---%zd %@", i, [NSThread currentThread]);
//signal
dispatch_semaphore_signal(_semaphore);
});
}
NSLog(@"=== end ---_semaphore ");
}
输出:
14:17:46[DM] === start _semaphore
14:17:46[DM] === mid ---_semaphore
14:17:46[DM] === end ---_semaphore
14:17:49[DM] === for 1 ---0 <NSThread: 0x600000308080>{number = 6, name = (null)}
14:17:52[DM] === for 1 ---1 <NSThread: 0x60000030e300>{number = 5, name = (null)}
14:17:55[DM] === for 1 ---2 <NSThread: 0x6000003c5100>{number = 10, name = (null)}
14:17:58[DM] === for 1 ---3 <NSThread: 0x600000308140>{number = 3, name = (null)}
14:18:01[DM] === for 1 ---4 <NSThread: 0x6000003c9400>{number = 11, name = (null)}
14:18:04[DM] === for 1 ---5 <NSThread: 0x60000030e200>{number = 4, name = (null)}
14:18:07[DM] === for 1 ---6 <NSThread: 0x6000003c0880>{number = 12, name = (null)}
14:18:10[DM] === for 1 ---7 <NSThread: 0x6000003c0c00>{number = 13, name = (null)}
14:18:13[DM] === for 1 ---8 <NSThread: 0x6000003c0f40>{number = 14, name = (null)}
14:18:16[DM] === for 1 ---9 <NSThread: 0x6000003c9400>{number = 15, name = (null)}
14:18:19[DM] === for 2 ---0 <NSThread: 0x6000003380c0>{number = 16, name = (null)}
14:18:22[DM] === for 2 ---1 <NSThread: 0x6000003c0c00>{number = 17, name = (null)}
14:18:25[DM] === for 2 ---2 <NSThread: 0x6000003c5640>{number = 18, name = (null)}
14:18:28[DM] === for 2 ---3 <NSThread: 0x6000003c55c0>{number = 19, name = (null)}
14:18:31[DM] === for 2 ---4 <NSThread: 0x6000003c6180>{number = 20, name = (null)}
14:18:34[DM] === for 2 ---5 <NSThread: 0x6000003c0500>{number = 21, name = (null)}
14:18:37[DM] === for 2 ---6 <NSThread: 0x600000319b00>{number = 22, name = (null)}
14:18:40[DM] === for 2 ---7 <NSThread: 0x600000308140>{number = 23, name = (null)}
14:18:43[DM] === for 2 ---8 <NSThread: 0x6000003c0800>{number = 24, name = (null)}
14:18:46[DM] === for 2 ---9 <NSThread: 0x6000003c0880>{number = 25, name = (null)}