zoukankan      html  css  js  c++  java
  • Grand Central Dispatch-thread pool pattern

    Grand Central Dispatch (GCD) is a technology developed by Apple Inc. to optimize application support for systems with multi-core processors and other symmetric multiprocessing systems.[2] It is an implementation of task parallelism based on the thread pool pattern. The fundamental idea is to move the management of the thread pool out of the hands of the developer, and closer to the operating system. The developer injects "work packages" into the pool oblivious of the pool's architecture. This model improves simplicity, portability and performance.

    GCD was first released with Mac OS X 10.6, and is also available with iOS 4 and above. The name "Grand Central Dispatch" is a reference to Grand Central Terminal.[citation needed]

    Design[edit]

    GCD works by allowing specific tasks in a program that can be run in parallel to be queued up for execution and, depending on availability of processing resources, scheduling them to execute on any of the available processor cores[11][12] (referred to as "routing" by Apple).[13]

    A task can be expressed either as a function or as a "block."[14] Blocks are an extension to the syntax of CC++, and Objective-C programming languages that encapsulate code and data into a single object in a way similar to a closure.[11] GCD can still be used in environments where blocks are not available.[citation needed]

    Grand Central Dispatch still uses threads at the low level but abstracts them away from the programmer, who will not need to be concerned with as many details. Tasks in GCD are lightweight to create and queue; Apple states that 15 instructions are required to queue up a work unit in GCD, while creating a traditional thread could easily require several hundred instructions.[11]

    A task in Grand Central Dispatch can be used either to create a work item that is placed in a queue or assign it to an event source. If a task is assigned to an event source, then a work unit is made from the block or function when the event triggers, and the work unit is placed in an appropriate queue. This is described by Apple as more efficient than creating a thread whose sole purpose is to wait on a single event triggering.

    Features[edit]

    The dispatch framework declares several data types and functions to create and manipulate them:

    • Dispatch Queues are objects that maintain a queue of tasks, either anonymous code blocks or functions, and execute these tasks in their turn. The library automatically creates several queues with different priority levels that execute several tasks concurrently, selecting the optimal number of tasks to run based on the operating environment. A client to the library may also create any number of serial queues, which execute tasks in the order they are submitted, one at a time.[12]Because a serial queue can only run one task at a time, each task submitted to the queue is critical with regard to the other tasks on the queue, and thus a serial queue can be used instead of a lock on a contended resource.
    • Dispatch Sources are objects that allow the client to register blocks or functions to execute asynchronously upon system events, such as a socket or file descriptorbeing ready for reading or writing, or a POSIX signal.
    • Dispatch Groups are objects that allow several tasks to be grouped for later joining. Tasks can be added to a queue as a member of a group, and then the client can use the group object to wait until all of the tasks in that group have completed.
    • Dispatch Semaphores are objects that allow a client to permit only a certain number of tasks to execute concurrently.

    https://en.wikipedia.org/wiki/Grand_Central_Dispatch

  • 相关阅读:
    DataTable转换成IList<T>的简单实现
    websocket
    获取用户使用设备信息
    判断对象是否相等
    关于PC适配
    树形数据结构实现平铺展示
    埋点
    多层表单验证
    表格行拖动,数据中状态值不同的禁止拖拽
    element tree 深度查询
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8313580.html
Copyright © 2011-2022 走看看