What are Threads in iOS…

Had enough of article on GCD and Queue management; let’s not dive into codes but let us summarise all about threads in iOS.

As said Threading happens inside the processor to prioritise things in our application. Depending on the processor the number of threads are allocated to a particular process. A single core processor will consist of a single thread having its own section of physical memory associated with it performing context switching over task. Depending upon the core; a process can have one or more threads (Multithreaded — uses Concurrency & Parrallelism).

Wait a minute! … What is a process?

Process is your application running in active state executing instruction at run time. Instructions are of assembly format which understands in binary digits; known as bits having two values 0 & 1. They can be grouped as 4, 8, 16, 32 or 64. Also, 1 byte = 8 bit.

Swift uses 64-bit numbers. To take an example of, the standard Int type is of 64 bits. Having the least significant bit(LSB) to the right side and the most significant bit(MSB) to the left side.

The LSB & MSB depends upon the endianness of an architecture. Whereas, endianness are of two type namely;

Apple’s own A processors is little-endian

While using swift standard library we don’t need to worry about the endianness but while working with low-level C library API we take into consideration as we deal with pointers towards register in memory location.

Lets describe it in the POSIX thread diagram:-

1. A Process having more than one thread in its memory

From Image 1 above; A thread consist of a Stack, collection of register with unique identifier, also known as properties.

In iOS, a process or application can consists of one or more threads. The operating system scheduler manages the threads independently of each other. Each thread can execute concurrently, but it’s up to the system to decide if this happens, when this happens, and how it happens.- Ray Wenderlich

It may look great to have several threads in an app but all comes with a cost and takes time while it gets created. Coming to the cost, The main thread has a fix stack size of 1MB which initiallly uses some space but can grow till 1MB depending on its usage and any other secondary thread has size of 512KB by default. One can change its size before starting the thread with minimum size of 16KB (always in a multiple of 4KB)

Max thread limit in iOS app is 64

To start with, The Main thread in iOS which is the starting point in any app.

2. Sequence diagram of App delegate lifecycle

As shown in image no 2. After the <<creation>>8: off Runloop; getting to 9:Run, drawing the current screen if needed denotes the starting point of the Main Thread. This Main Thread is responsible only to update the UI elements rest all heavy weightage task should be done on the background thread.

We can always check on which thread we are on using Thread.current and main thread using code Thread.current.isMainThread.

We can have a look at the number threads as shown in image 3 below; Run Application -> Show the debug navigator -> Pause the application.

3. Threads available

Summarising GCD on thread -

GCD (FIFO queues) is built on top of threads. Under the hood it manages a shared thread pool — Ray Wenderlich
All task controlled by GCD are on pool of threads which are managed by the system. Serial queue has one thread associated with it whereas Concurrent queue uses many thread available by the system. But, if the thread share the same process, sharing same address space which can read & write to same shared resource will lead to Race condition. One need to be careful while handling it as read & write are two separate task which depends on CPU clock cycles; wherein one tick of the clock allows one task at a time to execute. We can solve this by using Thread Barrier that when called to write a task completes before moving forward to read a task but after finishing all the read task that was submitted before a write task.

References:- https://www.w3.org/People/Frystyk/thesis/multithread.html

We will dive into codes in the next article…COMING SOON!

Thank you…

iOS Developer @TCS | Swift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store