前面我们说了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:

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.

翻译过来大概是说

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_async
  • dispatch_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的更多相关文章

  1. 玩转iOS开发:iOS中的GCD开发(三)

    上一章, 我们了解到了GCD里的一些队列和任务的知识, 也实践了一下, 同时我们也对主队列的一些小情况了解了一下, 比如上一章讲到的卡线程的问题, 如果没有看的朋友可以去看看玩转iOS开发:iOS中的 ...

  2. ios 中的 GCD

    摘自:http://www.cocoachina.com/swift/20150129/11057.html libdispatch是Apple所提供的在IOS和OS X上进行并发编程的库,而GCD正 ...

  3. iOS中的GCD线程

    一.什么是GCD      全称是Grand Central Dispatch ,纯C语言编写,提供非常多强大的函数,是目前苹果官网推荐的多线程开发方法,NSOperation 便是基于GCD的封装 ...

  4. ios中多线程GCD NSOperation NSThread 相关的操作解析

    //1.GCD 继承自C语言 优点 简单方便 //开启一个子线程处理耗时的操作 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIO ...

  5. IOS中的多线程之GCD

    在ios中,使用多线程有三种方式,分别是:NSThread.NSOperation和NSOperationQueue.GCD,在本节,主要讲解一下CDD的使用. GCD(Grand Central D ...

  6. iOS中GCD的使用小结

    http://www.jianshu.com/p/ae786a4cf3b1 本篇博客共分以下几个模块来介绍GCD的相关内容: 多线程相关概念 多线程编程技术的优缺点比较? GCD中的三种队列类型 Th ...

  7. iOS中的几种锁的总结,三种开启多线程的方式(GCD、NSOperation、NSThread)

    学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary OC中的几种锁 为什么要引入锁 ...

  8. iOS中多线程知识总结(一)

    这一段开发中一直在处理iOS多线程的问题,但是感觉知识太散了,所以就把iOS中多线程的知识点总结了一下. 1.基本概念 1)什么是进程?进程的特性是什么? 进程是指在系统中正在运行的一个应用程序.   ...

  9. 谈谈iOS中的锁

    1 前言 近日工作不是太忙,刚好有时间了解一些其他东西,本来打算今天上午去体检,但是看看天气还是明天再去吧,也有很大一个原因:就是周六没有预约上!闲话少说,这里简单对锁来个简单介绍分享. 2 目录 第 ...

  10. ios线程和GCD

    1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...

随机推荐

  1. 使用mysql_query()方法操纵数据库以及综合实例

    1.利用insert 语句添加记录 <? require('conn.php'); mysql_query( "insert into lyb ( title, content, au ...

  2. spoj ONP - Transform the Expression 中缀转后缀

    题目链接 将中缀表达式转化为后缀表达式. 数字的话直接放到答案的字符串里. 如果是左括号就进栈, 右括号就让栈里的符号都出来直到第一个左括号. 否则的话比较当前符号的优先级和栈顶符号的优先级. #in ...

  3. MySQL read_only选项的作用

    1作用: 从字面意思上看就可以知道这个是把mysql设置为只读,但是这个只读只是针对一般用户而言的,对于root这种用super权限的用户read_only是没有用的. 2设置方式: set glob ...

  4. poj2262---素数(质数)的判断

    收获:一开始以为是100万的所有数字,题目要求是只要偶数,也可以分析出来,如果是给一个奇数,当我们给他大于等于3的奇数(这个数加有可能不是质数,但至少满足是奇数,至于是不是质数还要自己判断),剪出来一 ...

  5. 错误解决一_call time pass-by-reference removed

    我的操作: 定义function my_function(&$param) 调用 my_function(&$value)错误来了:Call-time pass-by-referenc ...

  6. 接着上一个版本在上一个分离access-token和ticket的版本

    上代码: 本次修改将获取token和ticket分离出来,分别封装在函数中: 每个函数最后一个参数是一个回调参数: 回调函数的参数,是这一步中需要处理的结果; 结果怎么处理,根据传递进去的函数: va ...

  7. background-size:的认识;

    background-size:100%;其实是元素的背景图片的宽度和元素宽度相同,高度auto: 也可理解为:background-size:100% auto; 而background:cover ...

  8. express中路由设置的坑-----1

    router.get('/commodities/sortable', utils.logged, function (req, res) { Commodity.find({force_top:tr ...

  9. 7.15 css与js 选择奇偶子元素的区别

    js: 选取偶数位置的 <tr> 元素 $("tr:even") 选取奇数位置的 <tr> 元素 $("tr:odd") css 选取偶 ...

  10. iOS禁用部分文件ARC

    TARGETS的build Phases中的Compile Source里修改文件备注文件参数设定: 增加-fobjc-arc来使单个文件 支持ARC,或者添加-fno-objc-arc使单个文件不支 ...