Concurrency in swift
In the previous section of this tutorial, we have discussed the GCD concepts in Swift, which allows Multithreading in swift. In this section of this tutorial, we will discuss the concurrent execution of the threads in swift. We will also include the types of Queues used by GCD and the QoS classes.
As we have already mentioned, the Grand-Central-Dispatch (GCD) uses the Dispatch queue, which is used for task submission where the tasks are executed on a first-in-first-out basis. In iOS, the process or application contains one or more threads; it is the operating system's responsibility to schedule and manage the threads independently of each other.
However, each thread can be executed concurrently. The operating system decides which threads will execute concurrently, and when they will execute. One of the most used methods of archiving concurrency is Time Slicing, where each thread is given the same amount of time to be executed, and the system performs the context switch at every interval. The time-slicing is mostly archived in single-core systems.
On the other hand, the multi-core systems can use parallelism to execute multiple threads simultaneously. However, the GCD is built on the top of threads; it manages a shared thread pool where we can add blocks of code to dispatch queues. GCD always decides the amount of parallelism that is required based on the system and the available resources.
As we have already discussed in the previous section, the GCD operates on the dispatch queues. The dispatch queue in swift is an instance of DispatchQueue, where we submit the tasks to this queue. GCD executes the tasks in the dispatch queue in First in First Out order. Here, the dispatch queue is thread-safe, which can be accessed simultaneously by multiple threads.
However, the Queue can be concurrent or serial. As the name suggests, the serial queue executes only one thread at the given time. Here, then GCD decides the execution timing. We can never know the amount of time spent on the execution of a single task.
On the other hand, the concurrent queue executed multiple tasks at the same time. We can never know the amount of the time it takes to start the next tasks. The tasks will start executing in the same order they added. However, they may end in a different order. We cannot assume the number of threads running at a single time.
Here, it depends on GCD, when to start the thread. GCD also decides whether to run a task on a different core if the execution of two threads overlaps each other.
There are three types of a queue available for GCD.
When the tasks are sent to global queues, we specify a quality of service class property. The QoS classes determine the priority of the tasks and then allow GCD to execute them.
There are the following categories of QoS classes present in the system.
Background: these are the tasks that run in the application background. The user is not aware of these tasks. These tasks are used for prefetching, downloading, uploading, maintenance, and other similar tasks. This must be executed in the background global priority queue.