• 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:

    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_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

            

            //同步执行全局可用的串行队列

            dispatch_sync(dispatch_get_main_queue(), ^{

            

            

            });

        });

        上面是不是处处都包含着block
      有什么问题请多多指教
  • 相关阅读:
    装载:深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
    装载: Matlab 提取矩阵 某一行 或者 某一列 的方法
    编码规范的作用
    转载:奇异值分解(SVD) --- 线性变换几何意义(下)
    转载:奇异值分解(SVD) --- 线性变换几何意义(上)
    转载:LBP代码详细注释
    转载:纹理分类(一)全局特征
    转载:LBP的初步理解
    转载:双线性插值
    Matlab位运算操作
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3206484.html
Copyright © 2020-2023  润新知