转的gcd学习

iOS开发中,多线程应用方式一般有如下几种

1 比较高层的,封装好的API:NSThread。用来创建非常驻线程以及常驻线程,默认支持NSRunLoop机制。

2 比较高层的,封装好的API:NSOperation。用来管理他的是NSOperationQueue。每一个NSOperation都是一个独立的线程。同样你可以设置其为常住线程比方while(1)这种恶心的代码。

3 比较底层的,简单封装的 C函数形式的API:GCD---Grand Central Dispatch,据说这个是比较底层的,针对多核cpu做的多线程模型,效率比上面两个高一些。具体不太清楚,感兴趣的自己做实验,用事实说话吧。

4 通用的跨平台的线程模型:POSIX线程模型,也是C函数形式的API,pthread族。

下面,我具体谈谈我对GCD的一些理解,其它的一些东西,网上太多了,都是抄来抄去的,不过确实能带入门,还是表示感谢。

dispatch_queue_t 类型的这个queue到底是个什么东西,字面意思理解:首先是一个queue(队列),用来做什么的呢,存放operation(操作/计算模块)的,说白了就是存放函数的。

这一点理解可以从他的应用方式看得出来:

dispatch_asyn(myQueue,^(param1,param2){

//the fucntion body

NSLog(@"excuting the block body");

}));

那问题来了:假设 一瞬间 上面这个语句执行了100次,那这100个函数块儿到底是怎么个执行顺序呢?又是不是在同一个线程上执行的呢?

这就引出了 queue的类型:顺序queueDISPATCH_QUEUE_SERIAL和并发queueDISPATCH_QUEUE_CONCURRENT.

1 DISPATCH_QUEUE_SERIAL 测试

[objc] view plain copy

dispatch_queue_t serialQ = dispatch_queue_create("myserialq", DISPATCH_QUEUE_SERIAL);  


for (int i=0; i<10; i++)  

{  

    dispatch_async(serialQ, ^{  

NSLog(@"block%d thread %@",i,[NSThread currentThread]);  

    });  


}  

打印log如下:

2015-12-11 17:08:17.477 noarct[10712:242196] block0 thread {number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block1 thread {number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block2 thread {number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block3 thread {number = 2, name = (null)}

2015-12-11 17:08:17.478 noarct[10712:242196] block4 thread {number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block5 thread {number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block6 thread {number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block7 thread {number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block8 thread {number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block9 thread {number = 2, name = (null)}

2015-12-11 17:08:17.479 noarct[10712:242196] block10 thread {number = 2, name = (null)}

可见:DISPATCH_QUEUE_SERIAL 类型的queue上执行 多个代码块儿是,是顺序执行的,一个一个执行的,并且都是在同一个Thread上执行的。也就是说DISPATCH_QUEUE_SERIAL 只有一个线程。

2 DISPATCH_QUEUE_CONCURRENT 测试

[objc] view plain copy

dispatch_queue_t concurrentQ = dispatch_queue_create("myconq", DISPATCH_QUEUE_CONCURRENT);  


for (int i=0; i<10; i++)  

{  

    dispatch_async(concurrentQ, ^{  

NSLog(@"block%d thread %@",i,[NSThread currentThread]);  

    });  


}  

打出的log如下:

2015-12-11 17:13:12.842 noarct[10788:246299] block7 thread {number = 6, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246300] block8 thread {number = 10, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246287] block0 thread {number = 2, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246301] block9 thread {number = 11, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246296] block4 thread {number = 8, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246297] block5 thread {number = 9, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246298] block6 thread {number = 7, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246295] block3 thread {number = 5, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246288] block1 thread {number = 3, name = (null)}

2015-12-11 17:13:12.842 noarct[10788:246291] block2 thread {number = 4, name = (null)}

如果多运行几次,上面的顺序还会变化。我们可以看到,

DISPATCH_QUEUE_CONCURRENT 类型的并发queue,在装入多个函数模块的时候,他的执行不一定是根据装入顺序来的,也不一定是在同一个线程上执行的,至于他需要启动多少个线程来执行 他目前需要执行的函数块儿,是由系统自己决定的,上面看到瞬间加入10个计算单元到这个queue,没有任何其它负载的情况下,他会立即处理这10个计算单元,于是就启动了10个线程分别执行。假设,我们额外做一些其它计算,他会启动多少个线程呢,尝试如下

3

[objc] view plain copy

dispatch_queue_t concurrentQ = dispatch_queue_create("myconq", DISPATCH_QUEUE_CONCURRENT);  


for (int i=0; i<10; i++)  

{  

    dispatch_async(concurrentQ, ^{  

NSLog(@"block%d thread %@",i,[NSThread currentThread]);  

    });  

NSLog(@"main %@",[NSThread currentThread]);  

}  

打印log如下

2015-12-11 17:20:44.029 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.029 noarct[10851:249705] block0 thread {number = 2, name = (null)}

2015-12-11 17:20:44.029 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.029 noarct[10851:249706] block1 thread {number = 3, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249705] block2 thread {number = 2, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249706] block3 thread {number = 3, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249705] block4 thread {number = 2, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249706] block5 thread {number = 3, name = (null)}

2015-12-11 17:20:44.030 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.030 noarct[10851:249705] block6 thread {number = 2, name = (null)}

2015-12-11 17:20:44.112 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.112 noarct[10851:249705] block7 thread {number = 2, name = (null)}

2015-12-11 17:20:44.112 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.112 noarct[10851:249706] block8 thread {number = 3, name = (null)}

2015-12-11 17:20:44.112 noarct[10851:249661] main {number = 1, name = main}

2015-12-11 17:20:44.112 noarct[10851:249705] block9 thread {number = 2, name = (null)}

可见,在有其他计算负载的时候,这个并发queue就没有那么放肆的开启n个线程来做他事儿了,线程数量有所减少。

是否可以设置这种并发queue的并发最大执行数量呢?这个,感兴趣的同学可以自己研究。

好了,就暂时说这么多。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文用来介绍 iOS 多线程中 GCD 的相关知识以及使用方法。这大概是史上最详细、清晰的关于 GCD 的详细讲...
    花花世界的孤独行者阅读 3,486评论 0 1
  • 一、前言 上一篇文章iOS多线程浅汇-原理篇中整理了一些有关多线程的基本概念。本篇博文介绍的是iOS中常用的几个多...
    nuclear阅读 6,256评论 6 18
  • GCD简介 Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法...
    哦累哇滚筒洗衣机阅读 4,929评论 1 2
  • 1.多线程知识: 线程与队列的区别:线程是代码执行的路径,队列则是用于保存以及管理任务的,线程负责去队列中取任务进...
    一川烟草i蓑衣阅读 2,490评论 0 0
  • GCD 队列组:dispatch_group 有时候我们会有这样的需求:分别异步执行多个耗时任务,当多个耗...
    gpylove阅读 3,332评论 0 0

友情链接更多精彩内容