ios개발

<ios 운영체제> GCD - grand central disptach queue

studying develop 2020. 4. 9. 01:32

음 일단 잘 모르겠다. 그런데 생각보다 gcd를 이해할 수준일 수도 있을거 같다.

 

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

 

Grand Central Dispatch (GCD or libdispatch), 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.

 

음 일단 애플이 개발한거고??, 멀티 코어 프로세서들 그리고 smp 시스템을 제공한다. 스레드 풀 패턴으로 타스크 패럴리즘을 구현했다. 개발자 손에서 스레드 풀을 떼어냈다는데, 그리고 운영체제가 관리하도록함. 개발자는 "워크 패키지들"을 풀의 아키텍쳐에 대해 모르는 상태로 주입한다함. 이러한 모델은 간결성을 높여주고, 이식성, 성능을 좋게 향상 시킨다.

 

 

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]

 

음 그랜드 센트럴 터미널?? 일단 왜 터미널이라 하지

 

The source code for the library that provides the implementation of GCD's services, libdispatch, was released by Apple under the Apache License on September 10, 2009.[3] It has been ported[4] to the FreeBSD 8.1+,[5] MidnightBSD 0.3+, [6] Linux, and Solaris.[7][8] 2011 attempts to make libdispatch work on Windows were not merge into upstream.[9][10] Apple has its own port of libdispatch.dll for Windows shipped with Safari and iTunes, but no SDK is provided.

 

음 libdispatch ?? 

 

Since around 2017, the original libdispatch repository hosted by Nick Hutchinson[11] was deprecated in favor of a version that is part of the Swift core library created in June 2016. The new version supports more platforms, notably including Windows.

 

 

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[12][13] (referred to as "routing" by Apple).

 

gcd는 프로그램에서 실행이 병렬적으로 작동가능한  특정 타스크들을 허용함으로서 작동합니다, 사용 가능한 프로세싱 자원들에 따라 결정됩니다, 타스크들이 어떤 프로세서 코어에서든 사용 가능 하도록 스케줄링합니다. 애플은 이걸 라우팅이라 하네요.

 

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

 

타스크를 함수 또는 블록으로 표현할 수 있습니다. 블록들은 c,c++의 확장입니다. 클로저와 마찬가지로 데이터와 코드를 단일 객체로 감쌉니다. ??gcd는 여전히 브록들이 사용 불가능한 환경에서도 사용 가능합니다 ??;무슨말이지.

 

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.

 

gcd는 여전히 스레드들을 낮은 수준에서 사용합니다. 하지만 추상화하여 프로그래머 손에서 멀어지게 합니다. gcd 타스크들은 생성하고 큐잉 하기 가볍습니다; 애플은 gcd에 작업 한 작업 단위를 큐업하려면 15개의 명령어들이 필요하다 합니다, 반면 전통적 스레드는 수백개의 명령어들을 요구할 것이라 합니다. 

 

음 어떤 의미에서 말한건지 잘 이해가 안된다. 큐? 스케줄링 큐에 넣는걸 말하나? 이 부분은 리눅스에서 안해본거 같다.

 

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.

 

gcd 내부의 한개의 타스크를 이용해 큐에 위치시킬 하나의 작업 아이템을 생성하기 위해 만들거나 또는 이벤트 소스에 할당 할 수 있습니다. 만약 타스크를 이벤트 소스로 할당하면, 이벤트가 발생하면 작업 유닛은 블록이나 함수로 부터 생성됩니다, 그리고 작업 단위는 적절한 큐에 위치 됩니다.  애플은 스레드는 그 존재 목적이 단일 이벤트 트리거가 발생하는 것을 기다리는 것 보다 매우 효율적이라 묘사합니다.

 

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.[13] 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 descriptor being 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.

Libdispatch comes with its own object model, OS Object, that is partially compatible with the Objective-C model. As a result, its objects can be bridged toll-free to ObjC objects.

 

 

Applications[edit]

GCD is used throughout macOS (beginning with 10.6 Snow Leopard), and Apple has encouraged its adoption by macOS application developers. FreeBSD developer Robert Watson announced the first adaptation of a major open source application, the Apache HTTP Server, to use GCD via the Apache GCD MPM (Multi-Processing Module) on May 11, 2010, in order to illustrate the programming model and how to integrate GCD into existing, large-scale multi-threaded, applications. His announcement observed that the GCD MPM had one third to half the number of lines as other threaded MPMs.[19][20]

Internals[edit]

GCD is implemented by libdispatch, with support from pthreads non-POSIX extensions developed by Apple. Apple has changed the interface since its inception (in OS X 10.5) through the official launch of GCD (10.6), Mountain Lion (10.8) and recently Mavericks (10.9). The latest changes involve making the code supporting pthreads, both in user mode and kernel, private (with kernel pthread support reduced to shims only, and the actual workqueue implementation moved to a separate kernel extension).[21]

 

gcd를 libdipatch로 구현한다, pthreads non-posix 확장을 이용한다. 

 

One other systems, libdispatch implements its own workqueue using the system's own event facilities (epoll, kevent, or Windows NT). On macOS, kevent is used with the kernel workqueue.

 

다른 한 시스템들은, libdispatch는 system 자신의 event facilities(epoll,kevent 등)를 이용해 자신의 workqueue를 구현한다. 맥 운영체제에서, kevent를 kernel workqueue를 사용한다.