Multithreading in Swift

Concurrency in swift

Concurrency in swift

We covered the GCD ideas in Swift, which enable multithreading in Swift, in the previous section of this course. We’ll talk about Swift’s concurrent thread execution in this section of the course. We will also cover the many kinds of queues that GCD and QoS classes use.

As previously stated, the Dispatch queue, which is used for task submission and task execution on a first-in, first-out basis, is utilized by the Grand-Central-Dispatch (GCD). If an iOS process or application has one or more threads, the operating system is in charge of scheduling and managing them separately from one another.

All threads can, however, run simultaneously. Which threads run concurrently and when is determined by the operating system. Time Slicing is a popular technique for archiving concurrency in which every thread is allotted the same amount of time to run and the system switches contexts on a regular basis. Most time-slicing is stored on single-core computers.

However, parallelism in multi-core systems allows for the simultaneous execution of several threads. On top of threads, though, the GCD oversees a shared thread pool from which we can add code blocks to dispatch queues. The system and the resources at hand are always taken into consideration by GCD when determining the necessary level of parallelism.

Dispatch Queues

The GCD works with the dispatch queues, as we have just covered in the previous section. In Swift, the dispatch queue is an instance of DispatchQueue, to which tasks are added. First in First Out is the order in which GCD completes the tasks in the dispatch queue. In this case, multiple threads can use the dispatch queue simultaneously since it is thread-safe.

On the other hand, the Queue may be serial or concurrent. The serial queue, as its name implies, only runs one thread at a time. The timing of the execution is then determined by GCD. The whole amount of time required to complete a task is always unknown to us.

Concurrency in swift -

Conversely, the concurrent queue carried out several jobs simultaneously. We are never sure how long it will take to begin the following task. The tasks will begin to run in the order that they were added. They might, however, conclude in a different order. The quantity of threads operating concurrently is not something we can predict.

Concurrency in swift -

In this case, the thread’s start time is determined by GCD. When two threads are running concurrently, GCD determines whether to execute a job on a separate core.

For GCD, three different kinds of queues are accessible.

1. Main Queue: The main queue serially executes the thread while operating on the main thread.

2. Global Queue: The entire system uses the Global queue, which is a concurrent queue. Four global queues with varying priorities are in operation. The four priority levels are background, low, default, and high, with the background priority queue having the lowest priority.

3. Custom Queue: These are made by the developers. They may be serial or concurrent.

QoS Classes

There is a quality of service class property that we specify when the jobs are sent to global queues. The tasks are prioritized by the QoS classes, which then provide GCD permission to do them.

The system contains QoS classes in the following categories.

1. User Interactive: In order to obtain the best possible user experience when we start an iOS application, there are a few activities that must be completed right away. These are user-interactive chores that must be completed to provide a positive user experience. These tasks are necessary for UI updates, event handling, and light workloads as developers. The primary thread must be used to complete the user-interactive tasks.

2. User-Initiated: The user starts these tasks by navigating through the user interface. These are asynchronous activities that are meant to be utilized in applications when the user must wait for instant responses, like in some API calls. The high priority global queue is where user-initiated actions are completed.

3. Utility: they are protracted jobs that usually have a progress bar. These kinds of threads are utilized for I/O, networking, and calculations. These jobs are carried out in the global queue with low priority.

Background: these are the processes that the program performs in the background. These tasks are unknown to the user. Prefetching, downloading, uploading, maintenance, and other related duties are done with these tasks. This needs to be done in the global priority queue in the background.

Share this Doc

Concurrency in swift

Or copy link

Explore Topic