GCD使用dispatch_semaphore_t创建多线程网络同步请求

一、简介:

dispatch_semaphore_t:表示信号,生成信号的方法是

dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);

其中参数0表示该新生成信号的总的信号量为0个。

dispatch_semaphore_wait:信号等待,它像一个安保,比如小区规定最多只能进入3辆车,而进入一辆车后名额就会减少一个,当剩下的名额为0的时候,再有汽车说要进去时,就只能在外面等待了,直到有名额闲置出来了,才能开进小区。

dispatch_semaphore_signal:信号释放,当有一辆车从小区出来时,就腾出来了一个名额。

 二、验证

这三个常用方法解释就暂且到这里,下面用代码来验证下上面介绍的作用。

复制代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self syncAction1];
}

- (void)syncAction1{
    
    //使用GCD的信号量 dispatch_semaphore_t 创建同步请求
    dispatch_group_t group =dispatch_group_create();
    dispatch_queue_t globalQueue=dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, globalQueue, ^{
        dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
        //模拟网络多线程耗时操作
        dispatch_group_async(group, globalQueue, ^{
            sleep(3);
            NSLog(@"%@---block1结束。。。",[NSThread currentThread]);
            dispatch_semaphore_signal(semaphore);
        });
        NSLog(@"%@---1结束。。。",[NSThread currentThread]);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });
    dispatch_group_async(group, globalQueue, ^{
        dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
        
        //模拟网络多线程耗时操作
        dispatch_group_async(group, globalQueue, ^{
            sleep(3);
            NSLog(@"%@---block2结束。。。",[NSThread currentThread]);
            dispatch_semaphore_signal(semaphore);
        });
        
        NSLog(@"%@---2结束。。。",[NSThread currentThread]);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });

    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        NSLog(@"%@---全部结束。。。",[NSThread currentThread]);
        
    });
    
}
复制代码

运行APP,点击页面,打印的结果如下:

2016-12-22 23:45:36.633 GCDTest[852:27326] <NSThread: 0x7fce485154f0>{number = 2, name = (null)}---1结束。。。
2016-12-22 23:45:36.633 GCDTest[852:27328] <NSThread: 0x7fce48606f40>{number = 3, name = (null)}---2结束。。。
2016-12-22 23:45:39.633 GCDTest[852:27329] <NSThread: 0x7fce48512ea0>{number = 4, name = (null)}---block1结束。。。
2016-12-22 23:45:39.633 GCDTest[852:27330] <NSThread: 0x7fce484a9960>{number = 5, name = (null)}---block2结束。。。
2016-12-22 23:45:39.634 GCDTest[852:27328] <NSThread: 0x7fce48606f40>{number = 3, name = (null)}---全部结束。。。

结论:通过打印结果可以验证,

1.只有当剩余信号总量大于0时线程才能通过dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);方法,继续向下执行。

2.当dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);下面的代码无法执行处于等待情况下时,dispatch_semaphore_signal(semaphore);一旦执行,释放出自己的信号,则代码又可以愉快的往下执行了。

 

posted @   滴水微澜  阅读(2943)  评论(1编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示