首页 > 其他分享 >iOS GCD 和信号量 实现 生产者和消费者模式

iOS GCD 和信号量 实现 生产者和消费者模式

时间:2023-05-22 20:02:17浏览次数:35  
标签:group GCD iOS dispatch 信号量 semaphore async block wait


GCD提供两种方式支持dispatch队列同步,即dispatch组和信号量。


一、dispatch组(dispatch group

1. 创建dispatch组

dispatch_group_t group = dispatch_group_create();

2. 启动dispatch队列中的block关联到group中

dispatch_group_async(group, queue, ^{ 
   // 。。。 
 });

3. 等待group关联的block执行完毕,也可以设置超时参数

dispatch_group_wait(group, DISPATCH_TIME_FOREVER); 
 4. 为group设置通知一个block,当group关联的block执行完毕后,就调用这个block。类似dispatch_barrier_async。
 dispatch_group_notify(group, queue, ^{
   // 。。。 
 }); 
 5. 手动管理group关联的block的运行状态(或计数),进入和退出group次数必须匹配
 dispatch_group_enter(group);
 dispatch_group_leave(group);
 所以下面的两种调用其实是等价的, 
 A)
 dispatch_group_async(group, queue, ^{ 
   // 。。。 
 }); 
 B) 
 dispatch_group_enter(group);
 dispatch_async(queue, ^{
   //。。。
 (group);
 });
 所以,可以利用dispatch_group_enter、 dispatch_group_leave和dispatch_group_wait来实现同步,具体例子:http://stackoverflow.com/questions/10643797/wait-until-multiple-operations-executed-including-completion-block-afnetworki/10644282#10644282。
  
二、dispatch信号量(dispatch semaphore)
1. 创建信号量,可以设置信号量的资源数。0表示没有资源,调用dispatch_semaphore_wait会立即等待。
 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
2. 等待信号,可以设置超时参数。该函数返回0表示得到通知,非0表示超时。
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
3. 通知信号,如果等待线程被唤醒则返回非0,否则返回0。
 dispatch_semaphore_signal(semaphore);
 最后,还是回到生成消费者的例子,使用dispatch信号量是如何实现同步:
  
 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //消费者队列
 while
 (sem, dispatch_time(DISPATCH_TIME_NOW, 10*NSEC_PER_SEC))) //等待10秒
       continue;
     //得到数据
   }
 });
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //生产者队列
 while
     if (!dispatch_semaphore_signal(sem))
    {
      sleep(1); //wait for a while
      continue;
    }
    //通知成功
  }
});

标签:group,GCD,iOS,dispatch,信号量,semaphore,async,block,wait
From: https://blog.51cto.com/u_16124099/6326814

相关文章

  • iOS initWithFrame:frame] 与 [UIButton buttonWithType] 对比
    What'sthedifferencebetweenthefollowingtwocodesnippets?1.UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];button.frame=frame;2.UIButton*button=[[[UIButtonalloc]initWithFrame:frame]autorelease];-buttonWithType:UI......
  • iOS AES 256加密
    #import<Foundation/Foundation.h>@classNSString;@interfaceNSData(Encryption)-(NSData*)AES256EncryptWithKey:(NSString*)key;//加密-(NSData*)AES256DecryptWithKey:(NSString*)key;//解密@end#import"NSData+AES.h"#import&......
  • Unzipping Files In iOS Using ZipArchive
    Inthistutorial,IamgoingtodemonstratehowyoucanzipandunzipfilesfromwithinyouriOSapplications.WewillbeusingathirdpartylibrarycalledZipArchivetoachievethis.Whilethereareacouplesolutionsouttheretozipandunzipfiles,......
  • iOS base64 编码详解
    iOSbase64编码详解iOS中将NSData转为base64编码时有NSDataBase64EncodingOptionsNSDataBase64EncodingOptions有四个选项/****************Base64Options****************/typedefNS_OPTIONS(NSUInteger,NSDataBase64EncodingOptions){//Usezerooron......
  • 信号量和互斥锁详解 非MarkDown版
    [参考链接1](http://blog.chinaunix.net/uid-24612247-id-2305050.html)详细说明:```信号量强调的是线程(或进程)间的同步:“信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候......
  • iOS 内购详解
    iOSApp内购SKProductsRequestDelegate不调用原因及解决办法之前代码是这样写的://此时request没有强指针指向它这样会导致SKProductsRequestDelegate不调用但不是每次都不调用,只是偶尔出现不调用的情况这个问题不好发现SKProductsRequest*request=[[SKProductsReq......
  • 信号量和互斥锁详解 MarkDown版
    信号量和互斥锁详解参考链接1参考链接2参考链接3参考链接4详细说明:信号量强调的是线程(或进程)间的同步:“信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候,就阻塞在那里)。当信号量为单值信号量是,也......
  • iOS ijkplayer 播放器 消息循环 详解
    iOSijkplayer 消息循环_mediaPlayer=ijkmp_ios_create(media_player_msg_loop);//开始消息循环intmedia_player_msg_loop(void*arg){@autoreleasepool{IjkMediaPlayer*mp=(IjkMediaPlayer*)arg;__weakIJKFFMoviePlayerController*ffpControll......
  • iOS UITabBarController 典型应用
    -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlau......
  • iOS Apple Development Document 详解
    iOS官方开发文档https://developer.apple.com/library/prerelease/ios/navigation/点击打开链接再次标记一下。......