ios中的GCD
前面我们说了block中提到它用于多线程,而gcd则是其用于多线程的典型。gcd其全称(Grand Central Dispatch)
那到底什么叫gcd,官方的解释如下:
Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and OS X
翻译:
Grand Central Dispatch(GCD)包括语言的特点,运行库,和系统的完善,提供系统的,全面的改进支持并行执行代码在多核硬件在iOS和OS X。
ios里主要用到其的就是关于队列的管理和调度,说到队列这里有三种队列
GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Blocks submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes. GCD offers three kinds of queues:
Main: tasks execute serially on your application’s main thread
Concurrent: tasks are dequeued in FIFO order, but run concurrently and can finish in any order.
Serial: tasks execute one at a time in FIFO order
The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:
Calling
dispatch_mainCalling
UIApplicationMain(iOS) orNSApplicationMain(OS X)Using a
CFRunLoopRefon the main thread
Use concurrent queues to execute large numbers of tasks concurrently. GCD automatically creates three concurrent dispatch queues that are global to your application and are differentiated only by their priority level. Your application requests these queues using the dispatch_get_global_queue function. Because these concurrent queues are global to your application, you do not need to retain and release them; retain and release calls for them are ignored. In OS X v10.7 and later, you can also create additional concurrent queues for use in your own code modules.
Use serial queues to ensure that tasks to execute in a predictable order. It’s a good practice to identify a specific purpose for each serial queue, such as protecting a resource or synchronizing key processes. Your application must explicitly create and manage serial queues. It can create as many of them as necessary, but should avoid using them instead of concurrent queues just to execute many tasks simultaneously.
Important: GCD is a C level API; it does not catch exceptions generated by higher level languages. Your application must catch all exceptions before returning from a block submitted to a dispatch queue.
dispatch_asyncdispatch_async_fdispatch_syncdispatch_sync_fdispatch_afterdispatch_after_fdispatch_applydispatch_apply_fdispatch_once
翻译过来大概是说
GCD提供和管理FIFO(First In First Out)队列,您的应用程序可以在块对象的形式提交的任务。块调度队列在系统管理的线程池中执行。不能确保在哪个线程上执行任务。GCD提供三种队列
Main(全局的可用的串行队列):在主线程中串行执行任务
Concurrent(并行队列):队列按照先进先出的顺序,但同时运行,可以以任何顺序完成
Serial(串行队列不过是私有):在任意时候以先进先出的顺序执行
主要队列是自动创建的系统和应用程序的主线程关联。你的应用程序使用一个(只有一个)的调用提交的主要队列三块的方法:
先呼叫dispatch_main,也就是从子线程跳出到主线程
再呼叫main.h中的UIApplicationMain
最后调用主线程中的CFRunLoopRef
使用并行队列执行大量的任务的同时。GCD自动创建三个并发调度队列是全局应用程序,只有它们的优先级区分。您的应用程序请求这些队列使用dispatch_get_global_queue功能。因为这些并行的队列是覆盖到您的应用程序,您不需要保留和释放;retain和release,他们被忽略。在OS X v10.7或以后,你还可以创建您自己的代码模块使用额外的并发队列
使用串行队列来确保任务按一定顺序执行。这是一个很好的实践来确定每个串行队列特定目的,如保护资源或同步的关键过程。您的应用程序必须显式地创建和管理串行队列。它可以创建必要的许多人,但应避免使用它们而不是并发队列只执行许多任务同时。
GCD是一个C级API;它不抓捕的更高水平的语言产生异常。你的应用程序必须在提交给调度队列块之前返回所有异常。
dispatch_asyncdispatch_async_f比较,它们之音的区别就是多了一个_f,再就是用_f多了一个参数,其它的好像没什么区别,官网上也没查到
//这里用的是串行队列
dispatch_queue_t que=dispatch_queue_create("123",NULL);
//异步执行私有调度队列
dispatch_async(que, ^{
//这里是当上面的内容执行完马上执行,即同步执行
dispatch_sync(dispatch_get_main_queue(), ^{
});
//这里不确定什么时候能执行即异步中的异步
// dispatch_async(dispatch_get_main_queue(), ^{
//
// });
});
//释放
dispatch_release(que);
//这里是异步执行并发行队列
), ^{
//同步执行全局可用的串行队列
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
上面是不是处处都包含着block
ios中的GCD的更多相关文章
- 玩转iOS开发:iOS中的GCD开发(三)
上一章, 我们了解到了GCD里的一些队列和任务的知识, 也实践了一下, 同时我们也对主队列的一些小情况了解了一下, 比如上一章讲到的卡线程的问题, 如果没有看的朋友可以去看看玩转iOS开发:iOS中的 ...
- ios 中的 GCD
摘自:http://www.cocoachina.com/swift/20150129/11057.html libdispatch是Apple所提供的在IOS和OS X上进行并发编程的库,而GCD正 ...
- iOS中的GCD线程
一.什么是GCD 全称是Grand Central Dispatch ,纯C语言编写,提供非常多强大的函数,是目前苹果官网推荐的多线程开发方法,NSOperation 便是基于GCD的封装 ...
- ios中多线程GCD NSOperation NSThread 相关的操作解析
//1.GCD 继承自C语言 优点 简单方便 //开启一个子线程处理耗时的操作 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIO ...
- IOS中的多线程之GCD
在ios中,使用多线程有三种方式,分别是:NSThread.NSOperation和NSOperationQueue.GCD,在本节,主要讲解一下CDD的使用. GCD(Grand Central D ...
- iOS中GCD的使用小结
http://www.jianshu.com/p/ae786a4cf3b1 本篇博客共分以下几个模块来介绍GCD的相关内容: 多线程相关概念 多线程编程技术的优缺点比较? GCD中的三种队列类型 Th ...
- iOS中的几种锁的总结,三种开启多线程的方式(GCD、NSOperation、NSThread)
学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary OC中的几种锁 为什么要引入锁 ...
- iOS中多线程知识总结(一)
这一段开发中一直在处理iOS多线程的问题,但是感觉知识太散了,所以就把iOS中多线程的知识点总结了一下. 1.基本概念 1)什么是进程?进程的特性是什么? 进程是指在系统中正在运行的一个应用程序. ...
- 谈谈iOS中的锁
1 前言 近日工作不是太忙,刚好有时间了解一些其他东西,本来打算今天上午去体检,但是看看天气还是明天再去吧,也有很大一个原因:就是周六没有预约上!闲话少说,这里简单对锁来个简单介绍分享. 2 目录 第 ...
- ios线程和GCD
1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...
随机推荐
- 使用verilog实现4选1数据选择器的几种方法
第一种方法module mux( d1, d2, d3, d4, se1, se2, dout ); input d1; input d2; input d3; input d4; input se1 ...
- linux开机自动挂载NTFS-WINDOWS分区
1.安装ntfs-3g-2009.4.4.tgz 2.输入fdisk -l 看一下分区 由此可见:/dev/sda5,6,7 即是windows下的D,E,F盘(NTFS格式). 3.vim /etc ...
- 基本数据结构简介--ath9k网卡驱动开发总结(二)
ath9k驱动代码主要数据结构概览. (1)在ath9k的驱动中,几乎是最顶层的数据结构是ath_softc,这个数据结构几乎随处可见.ath_softc是硬件与MAC层进行交互的中间载体,很多有用的 ...
- Asp.net MVC学习--默认程序结构、工作流程
二.MVC 默认程序结构 MVC新建好之后,会对应的出现几个包,分别是:Controller.Model.View --即MVC 其中的默认的Default.aspx文件可以方便url重写,如果不设置 ...
- NET Core 的 Views
NET Core 十种方式扩展你的 Views 原文地址:http://asp.net-hacker.rocks/2016/02/18/extending-razor-views.html作者:Jür ...
- Oracle EBS-SQL (GL-2):从总帐追溯到库存
SELECT je_header_id,je_line_num,trx_class_name, trx_type_name, trx_number_displayed, trx_date, comme ...
- 1.9 需求订单导入MDS
1.9 需求订单导入MDS 1.9.1 业务方案描述 将”需求订单维护表”中完成调整维护的需求订单导入系统标准MDS中,使之驱动对应的物料需求计划(MRP)的运行. 1.9.2 ...
- Cortex-M3学习日志(二)-- 按键实验
有输出总会有输入,今天测试一下按键的功能,第一节已经说过了与GPIO端口相关的寄存器,这里不在重复,想要从端口读取数据,首先把FIODIR这个寄存器设置为输入,再从FIOPIN寄存器读取数据就可以了, ...
- mybatis.generator.configurationFile
mybatis.generator.configurationFile 有一个更好的配置方法,可以不用在generateConfig.xml里面写死驱动的地址:如果你的mybatis连接也是在pom. ...
- [原]基于CAS实现单点登录(SSO):登录成功后,cas client如何返回更多用户信息
从cas server登录成功后,默认只能从casclient得到用户名.但程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况. cas client拿到用户名后再到数据库中查 ...