Swift 3.0— Concurrent Programming with GCD

In an operation systems concurrency is the execution of the several process threads running in parallel. These threads can communicate with each other through either shared memory or message passing.

Grand Central Dispatch (GCD) is an implementation of task parallelism based on the thread pool pattern. It is a low-level C API that works with block objects. GCD let dispatch tasks to multiple cores.

Types of dispatch queues:

Main Queue — performs all it’s tasks on the main thread. All UI-related tasks are running on the main queue.

Dispatch on the main thread

Concurrent Queue — can be retrieved from GCD in order to execute synchronous and asynchronous. These queues are global to the application and differentiated only by their priority level. It calls Global Dispatch queues.

Creating concurrent queue and dispatch code synchronously and asynchronously.
Concurrent queue with asynchronous dispatch code

Serial Queues — will always execute they task in FIFO (first-in-first-out) way, executing only one block at the time. Series Queues are not blocking the main thread. Serial queues are great for managing a shared resource. It provides guaranteed serialized access to the shared resource and prevents race conditions. The default DispatchQueue is serial.

Serial queue
Serial queue with dispatch code asynchronously
Serial queue with dispatch code synchronously

Multiple dispatch queues can be used during the lifetime of your application at the same time. System has one main queue, multiple serial, and, multiple concurrent queues.

Block objects (closures) submitted to GCD don’t return the value. Block objects copy the values of local variables and keep those copies for the local use. A block objects can be passed to GCD and it will be executed synchronous or asynchronous depending on the method the programmer will choice. Operations can be configured to run manually or be placed on operation queues.

Operation and OperationQueue are build on the top of the GCD

Types of operations:

  1. Block operations — executes one or more block objects.
  2. Plain operations — required to be subclasses, where code to be executed will be written inside the main method of the operation objects.
  3. Invocation operations — invokes a method in another, existing object.

Operations can be managed with operation queues which have the data type OperationQueue. Operation adds a little extra overhead compared to GCD, but you can add dependency among various operations and re-use, cancel or suspend them.

Common Mistake while working with queues and operation queues:

Deadlock happens when first operation depends on the second operation while second operation is waiting execution of the first operation. It’s important to not create interdependent operations to prevent application from hanging, wait forever, and taking up memory!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.