iOS GCD-信号量(semaphore)

释放双眼,带上耳机,听听看~!

GCD 信号量,主要有三个方法:

dispatch_semaphore_create(long value);  创建信号量 ,value代表同一时间执行的线程数。

dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);第一个参数是信号量,第二个参数:

                DISPATCH_TIME_NOW,超时时间从现在开始,表示忽略信号量,直接开始执行

               DISPATCH_TIME_FOREVER   超时时间一直到未来,一直等待信号量大于0,会阻塞线程。

等待信号量,信号量大于0的时候会执行减一操作,等于0的时候会阻塞线程。

 dispatch_semaphore_signal(dispatch_semaphore_t dsema);  发送信号量,该函数会对信号量加一操作

-(void)dispatchSemaphore{
//这种就是控制线程的最大并发数
//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(@"任务1:%@",[NSThread currentThread]);
sleep(1);
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(@"任务2:%@",[NSThread currentThread]);
sleep(1);
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(@"任务3:%@",[NSThread currentThread]);
sleep(1);
NSLog(@"run task 3");
sleep(1);
NSLog(@"complete task 3");
dispatch_semaphore_signal(semaphore);
});
//任务4
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"任务4:%@",[NSThread currentThread]);
sleep(1);
NSLog(@"run task 4");
sleep(1);
NSLog(@"complete task 4");
dispatch_semaphore_signal(semaphore);
});
}

“2”:由于设定的信号值为2,先执行两个线程,等执行完一个,才会继续执行下一个,保证同一时间执行的线程数不超过2。允许同时并发的操作最多只有两次,当并发量达到2以后,信号量就就减小到0,这时候wait操作会起作用,DISPATCH_TIME_FOREVERs 表示会一直等待,一直等待信号量大于0,也就是两个任务完成一个了,这时信号量又+1,那么就可以再次执行一个任务这个方法相当于是控制线程的最大的并发数。

打印

2018-11-26 16:37:39.611215+0800 yymodelText[79901:1540463] 任务2:<NSThread: 0x600002d0b180>{number = 4, name = (null)}
2018-11-26 16:37:39.611215+0800 yymodelText[79901:1539245] 任务1:<NSThread: 0x600002d0a880>{number = 3, name = (null)}
2018-11-26 16:37:40.616031+0800 yymodelText[79901:1540463] run task 2
2018-11-26 16:37:40.616036+0800 yymodelText[79901:1539245] run task 1
2018-11-26 16:37:41.616440+0800 yymodelText[79901:1539245] complete task 1
2018-11-26 16:37:41.616440+0800 yymodelText[79901:1540463] complete task 2
2018-11-26 16:37:41.616954+0800 yymodelText[79901:1540464] 任务3:<NSThread: 0x600002d15280>{number = 5, name = (null)}
2018-11-26 16:37:41.616954+0800 yymodelText[79901:1540465] 任务4:<NSThread: 0x600002d14f00>{number = 6, name = (null)}
2018-11-26 16:37:42.617745+0800 yymodelText[79901:1540464] run task 3
2018-11-26 16:37:42.617779+0800 yymodelText[79901:1540465] run task 4
2018-11-26 16:37:43.621214+0800 yymodelText[79901:1540464] complete task 3
2018-11-26 16:37:43.621238+0800 yymodelText[79901:1540465] complete task 4

可以看出最多只有两个线程在执行任务。任务完成后,才继续执行。

用GCD的信号量来实现异步线程同步操作

    //依赖请求,第一个任务完成以后,在执行第二个,在执行第三个,相当于是依赖请求,
dispatch_semaphore_t  sem = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"任务1:%@",[NSThread currentThread]);
NSLog(@"run task 1");
sleep(1);
NSLog(@"complete task 1");
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"任务2:%@",[NSThread currentThread]);
NSLog(@"run task 2");
sleep(1);
NSLog(@"complete task 2");
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"任务3:%@",[NSThread currentThread]);
NSLog(@"run task 3");
sleep(1);
NSLog(@"complete task 3");
dispatch_semaphore_signal(sem);
});

创建一个初始值为0的信号量,当任务1完成的时候,才会发送信号量,这时候的信号里是1,wait才不会阻塞任务2的线程,同时信号量-1,只有任务2完成后,发送信号量,信号里才能大于1。wait方法才不会阻止任务三的线程,否则一直会等待任务2的完成,会阻塞线程。

打印

2018-11-26 16:47:59.801638+0800 yymodelText[81209:1570174] 任务1:<NSThread: 0x600003256dc0>{number = 5, name = (null)}
2018-11-26 16:47:59.801842+0800 yymodelText[81209:1570174] run task 1
2018-11-26 16:48:00.802180+0800 yymodelText[81209:1570174] complete task 1
2018-11-26 16:48:00.802409+0800 yymodelText[81209:1570174] 任务2:<NSThread: 0x600003256dc0>{number = 5, name = (null)}
2018-11-26 16:48:00.802521+0800 yymodelText[81209:1570174] run task 2
2018-11-26 16:48:01.803262+0800 yymodelText[81209:1570174] complete task 2
2018-11-26 16:48:01.803484+0800 yymodelText[81209:1570174] 任务3:<NSThread: 0x600003256dc0>{number = 5, name = (null)}
2018-11-26 16:48:01.803603+0800 yymodelText[81209:1570174] run task 3
2018-11-26 16:48:05.505578+0800 yymodelText[81209:1561334] 任务1:<NSThread: 0x600003256bc0>{number = 4, name = (null)}
2018-11-26 16:48:05.505696+0800 yymodelText[81209:1561334] run task 1
2018-11-26 16:48:05.740506+0800 yymodelText[81209:1570292] complete task 3
2018-11-26 16:48:06.507895+0800 yymodelText[81209:1561334] complete task 1
2018-11-26 16:48:06.508307+0800 yymodelText[81209:1570292] 任务2:<NSThread: 0x600003256e80>{number = 6, name = (null)}
2018-11-26 16:48:06.508510+0800 yymodelText[81209:1570292] run task 2
2018-11-26 16:48:07.513103+0800 yymodelText[81209:1570292] complete task 2
2018-11-26 16:48:07.513580+0800 yymodelText[81209:1570292] 任务3:<NSThread: 0x600003256e80>{number = 6, name = (null)}
2018-11-26 16:48:07.513840+0800 yymodelText[81209:1570292] run task 3
2018-11-26 16:48:08.515507+0800 yymodelText[81209:1570292] complete task 3

第二个打印的,可以来看出,他还是有可能开辟出新的线程,毕竟是异步的

在此总结下,同步和异步决定了是否开启新线程(或者说是否具有开启新线程的能力),串行和并发决定了任务的执行方式——串行执行还是并发执行(或者说开启多少条新线程)。

人已赞赏
iOS文章

iOS 多个网络请求成功以后,在回调给H5

2020-3-2 18:04:09

iOS文章

iOS 图片添加水印

2020-3-2 19:59:15

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索