GCD与多线程

GCD,全称Grand Central Dispath,是苹果开发的一种支持并行操作的机制。它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务。

GCD中的FIFO队列称为dispatch queue,它可以保证先进来的任务先得到执行(但不保证一定先执行结束)。

通过与线程池的配合,dispatch queue分为下面两种:

      • Serial Dispatch Queue -- 线程池只提供一个线程用来执行任务,所以后一个任务必须等到前一个任务执行结束才能开始。
      • Concurrent Dispatch Queue -- 线程池提供多个线程来执行任务,所以可以按序启动多个任务并发执行。

        1. Basic Management

        我们可以通过dispatch_queue_cretae来创建队列,然后用dispatch_release释放。比如下面两段代码分别创建串行队列和并行队列:

        1. dispatch_queue_t serialQ = dispatch_queue_create("eg.gcd.SerialQueue", DISPATCH_QUEUE_SERIAL);
        2. dispatch_async(serialQ, ^{
        3. // Code here
        4. });
        5. dispatch_release(serialQ);
        6. dispatch_queue_t concurrentQ = dispatch_queue_create("eg.gcd.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
        7. dispatch_async(concurrentQ, ^{
        8. // Code here
        9. });
        10. dispatch_release(concurrentQ);

        而系统默认就有一个串行队列main_queue和并行队列global_queue:

        1. dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        2. dispatch_queue_t mainQ = dispatch_get_main_queue();

        通常,我们可以在global_queue中做一些long-running的任务,完成后在main_queue中更新UI,避免UI阻塞,无法响应用户操作:

        1. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        2. // long-running task
        3. dispatch_async(dispatch_get_main_queue(), ^{
        4. // update UI
        5. });
        6. });

        上面提到dispatch_async这个接口,用来提交blcok给指定queue进行异步执行。这个接口会在成功提交block后立即返回,然后继续执行下去。由于block是定义在栈上的,所以需要将其复制到堆上,见这里

        与之相对应的是dispatch_sync接口,提交block以供同步执行。这个接口会等到block执行结束才返回,所以不需要复制block。So,如果在调用该接口在当前queue上指派任务,就会导致deadlock。维基百科上给了段示例代码:

        1. dispatch_queue_t exampleQueue = dispatch_queue_create("com.example.unique.identifier", NULL );
        2. dispatch_sync( exampleQueue,^{
        3. dispatch_sync( exampleQueue,^{
        4. printf("I am now deadlocked...\n");
        5. });});
        6. dispatch_release( exampleQueue );

        如果追求的是并发,那么dispatch_sync有什么用呢?关于dispatch_sync的用途,StackOverFlow是这样讨论的:

        Can anyone explain with really clear use cases what the purpose of dispatch_sync in GCD is for? I can't understand where and why I would have to use this.

        高手回答:

        You use it when you want to execute a block and wait for the results.

        One example of this is the pattern where you're using a dispatch queue instead of locks for synchronization. For example, assume you have a shared NSMutableArray a, with access mediated by dispatch queue q. A background thread might be appending to the array (async), while your foreground thread is pulling the first item off (synchronously):

        NSMutableArray*a =[[NSMutableArray alloc] init];// All access to `a` is via this dispatch queue!dispatch_queue_t q = dispatch_queue_create("com.foo.samplequeue", NULL);
        
        dispatch_async(q,^{[a addObject:something];});// append to array, non-blocking
        
        __block Something*first = nil;// "__block" to make results from block available
        dispatch_sync(q,^{// note that these 3 statements...if([a count]>0){// ...are all executed together...
        first =[a objectAtIndex:0];// ...as part of a single block...[a removeObjectAtIndex:0];// ...to ensure consistent results}});

        下面附上一个Demo  :(后台队列下载图片,当下载完后,调用主线程在前台显示)

        在Concoller中:

        1. -(void)viewDidAppear:(BOOL)animated{
        2. dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        3. dispatch_async(concurrentQueue, ^{
        4. __block UIImage *image = nil;
        5. dispatch_sync(concurrentQueue, ^{
        6. /* 下载图片 */
        7. NSString *urlAsString = @"http://images.apple.com/mac/home/images/promo_lead_macbook_air.jpg";
        8. NSURL *url = [NSURL URLWithString:urlAsString];
        9. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        10. NSError *downloadError = nil;
        11. NSData *imageData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&downloadError];
        12. if (downloadError == nil && imageData != nil){
        13. image = [UIImage imageWithData:imageData];
        14. } else if(downloadError!=nil){
        15. NSLog(@"Error happened = %@", downloadError);
        16. }else {
        17. NSLog(@"No data could get downloaded from the URL.");
        18. }
        19. });
        20. //在主队列中把图片展示给用户
        21. dispatch_sync(dispatch_get_main_queue(), ^{
        22. if (image != nil){
        23. /* 创建一个UIImageView */
        24. UIImageView *imageView = [[UIImageView alloc]
        25. initWithFrame:self.view.bounds];
        26. /* 设置图片*/
        27. [imageView setImage:image];
        28. /* 设置图片比例*/
        29. [imageView setContentMode:UIViewContentModeScaleAspectFit];
        30. /* 添加视图 */
        31. [self.view addSubview:imageView];
        32. } else {
        33. NSLog(@"Image isn't downloaded. Nothing to display.");
        34. } });
        35. });
        36. }

        运行结果:

GCD与多线程的更多相关文章

  1. iOS-----使用GCD实现多线程

    使用GCD实现多线程 GCD的两个核心概念如下: 队列 队列负责管理开发者提交的任务,GCD队列始终以FIFO(先进先出)的方式来处理任务---但 由于任务的执行时间并不相同,因此先处理的任务并一定先 ...

  2. apple平台下的objc的GCD,多线程编程就是优雅自然。

    在apple的操作系统平台里,GCD使得多线程编程是那么的优雅自然.在传统的多线程编程中,首先要写线程处理循环:之后还有事件队列,消息队列:还要在线程循环中分离事件解释消息,分派处理:还要考虑线程间是 ...

  3. GCD的多线程实现方式,线程和定时器混合使用

    GCD (Grand Central Dispatch) 性能更好,追求性能的话 1.创建一个队列 //GCD的使用 //创建一个队列 dispatch_queue_t queue = dispatc ...

  4. iOS开发中GCD在多线程方面的理解

    GCD为Grand Central Dispatch的缩写. Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法.在Mac OS X 10.6雪豹中 ...

  5. [转]关于GCD与多线程

    GCD是什么,你知道吗?你知道了GCD,你确定你会使用吗? 这一篇文章是站在初学者角度去分析GCD,原因是这个很多iOS开发者根本就没用过,即使用过,不知道其中的原理.讲解之前认识一下什么是线程,为什 ...

  6. GCD网络多线程---同步运行,异步运行,串行队列,并行队列

    总结:同步(无论是串行还是并行)----不又一次开辟子线程 异步(无论是串行还是并行)----开辟子线程 GCD: dispatch queue 主线程的main queue 并行队列 global ...

  7. GCD死锁 多线程

    NSLog("); dispatch_sync(dispatch_get_main_queue(), ^{ // sync同步 main串行 // 同步,异步--线程 同步-主线程 // m ...

  8. IOS学习之路八(GCD与多线程)

    GCD,全称Grand Central Dispath,是苹果开发的一种支持并行操作的机制.它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务. GCD中的FIFO队列称为 ...

  9. GCD实现多线程 实践

    GCD中弹窗的正确写法 - (void)viewDidLoad { //…… if (![self isStartLoading]) [self startLoading:nil]; //loadin ...

随机推荐

  1. C# Parse和Convert的区别分析(转)

    大家都知道在进行类型转换的时候有连个方法供我们使用就是Convert.to和*.Parse,但是疑问就是什么时候用C 什么时候用P 通俗的解释大家都知道: Convert 用来转换继承自object类 ...

  2. Redis源代码分析(一)--Redis结构解析

    从今天起,本人将会展开对Redis源代码的学习,Redis的代码规模比較小,很适合学习,是一份很不错的学习资料,数了一下大概100个文件左右的样子,用的是C语言写的.希望终于能把他啃完吧,C语言好久不 ...

  3. ZOJ 3802 Easy 2048 Again 像缩进DP

    链接:problemId=5334">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5334 题意:一个长度为5 ...

  4. Cocos2d-x3.0之路--02(引擎文件夹分析和一些细节)

    关于怎么搭建好开发环境的我就不写了,网上非常多. 那么 我们来看看 引擎文件的文件夹 所谓知己知彼 百战不殆嘛 先说一下setup.py 这个文件是有关配置的python文件,比方我们在进行andro ...

  5. dom03

    鼠标事件: 键盘事件: //通过class获取元素,封装一个通过class获取元素的方法 //IE10以下不支持document.getElementByClass() function getByC ...

  6. bluetooth发展(五岁以下儿童)------蓝牙功能测试(一个)

    newton板已出版.下面再组织我调试的一小方面,,蓝牙功能的实现和测试: 转载请注明出处:http://blog.csdn.net/wang_zheng_kai 以下是我写的newton开发板中bl ...

  7. API接口开发简述示例

    作为最流行的服务端语言PHP(PHP: Hypertext Preprocessor),在开发API方面,是很简单且极具优势的.API(Application Programming Interfac ...

  8. JS解析DataSet.GetXML()方法产生的xml

    在实际的项目制作过程中,经常要采用ajax方式来进行,当然,这就免不了要进行数据交换.如果采用拼接字符串的方式来进行,不仅拼接的时候麻烦,而且在拆解的时候更加麻烦,一旦遇到特殊字符,那么就是灾难了.因 ...

  9. MVC 使用jQuery上传文件

    在ASP.NET MVC Framework中,上传文件真是超级简单,看代码: View <formaction="<%=Url.Action("Process&quo ...

  10. Spring Resource之ResourceLoaderAware接口

    ResourceLoaderAware接口是一个特殊的标记接口,它表示对象需要提供给一个ResourceLoader引用: public interface ResourceLoaderAware { ...