前面我们说了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. Window及document对象的区别

    一.Window对象 -------------------------------------------------- ------------------- 对象属性 window //窗户自身 ...

  2. xunsearch安装与卸载

    刚接触xunsearch(迅搜)的时候,我是排斥的.排斥的原因不是因为害怕学习新技术(其实我是对心技术很感兴趣),而是因为:一方面xunsearch是国人开发的,对于国人写的开源产品,我不是太感兴趣( ...

  3. 异步流程控制库GoWithTheFlow

    异步流程控制库GoWithTheFlow 一个尾触发方式来控制异步流程的库, 有seq(顺序执行) par(同步执行) 两种方法 博客 http://notes.jetienne.com/2011/0 ...

  4. Ant Table组件

    http://www.cnblogs.com/hujunzheng/p/5689650.html React中使用Ant Table组件   v一.Ant Design of React http:/ ...

  5. android 自定义控件,自定义属性设置

    做listView的上拉下拉刷新,网上找了个历程.但是有些界面只有上拉刷新,有些界面是下拉刷新.觉得应该在xml里定义一个属性控制上下拉使能. 0.关于自定义控件: 自定义控件设计主要方式有:a) 继 ...

  6. lint使用简介

    LINT工具是一种软件质量保证工具,许多国外的大型专业软件公司,如微软公司,都把它作为程序检查工具,在程序合入正试版本或交付测试之前一定要保证通过了LINT检查,他们要求软件工程师在使用LINT时要打 ...

  7. select option 下拉多选单选bootstrap插件使用总结

    <select id="example-getting-started" multiple="multiple"> <option value ...

  8. Data visualization 课程 笔记3

    Learn how humans work to create a more effective computer interface 三种reasoning的方式  Deductive Reason ...

  9. 茴香豆的第五种写法---设置ExpandableListView系统自带图标按下效果

    1 编写groupindicator_selector.xml如下: <?xml version="1.0" encoding="utf-8"?> ...

  10. IOS开发:xcode5版本引发的问题

    下面这段代码是用于处理ios7头部透明问题的 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 if ( IOS7_OR_LATER ) { self.e ...