Concurrency in iOS

Gaurav Prakash
4 min readAug 6, 2022

--

What’s Concurrency ?

Suppose a person working in a call centre and handling multiple calls in same time, In Programming terms we can say execution of multiple instructions at the same time.

Suppose we have two tasks [ Task 1] -> [ Task 2] and both of them are being done at the same time or atleast it’s appearing that both of them are taking places at same time we can say that tasks are executing concurrently or phenomena is Concurrency.

Concurrency vs Parallelism

In Concurrency Actually in above example we can see A telephone operator handling multiple calls in one time but actually was answering one call at a time

Whereas In Parallelism multiple processing elements simultaneously for solving any problem . Sure It gives more speed but involves more processes.

Let’s understand with help of diagram :

Pros and cons of Parallelism : Obviously high speed but multiple resource required

How Concurrency is Achieved ?

Let’’s deep into some OS concepts like Time Slicing and Context Switching there are more fundamentals like Interruptions, Scheduling Algorithm, Latency, Time Quantum .

Context Switching : It basically means switching of the context and Context in a nutshell is a collection of values which needs to be loaded in various program register like Program Counter Register, Stack Pointer Register and various of the register in order to start or resume the execution of thread .

Time Slicing : It’s basically the period which scheduler grants to each thread before Preemption occurs , Preemption is simply pausing of the execution so if a thread is executed and if it’s paused after a certain duration of time we will say that particular Time Quantum and Context Switch takes place after that duration .

Let’s understand with this diagram how Context Switching and Time Slicing works, so I hope the illusion which was multiple tasks occuring at the same time is disappearing :)

For Example, We have three processes(P1, P2, P3) with their corresponding burst times(1ms, 4ms, 5ms). A rule of thumb is that 80% of CPU bursts should be smaller than the time quantum. Considering time slice of 2ms.
Here’s how CPU manages it by time slicing.

Time Slicing approach for process management

I hope you understand how Concurrency is achieved using Time Slicing, Context Switching, Time Quantam, Scheudling Algorithm and so on..

Main problems with Concurrency : Data Inconsistency

Concurrency in iOS is achieved using Multithreading , we have following options available

  • Achieved Multithreading by creating threads manually
  • Grand Central Dispatch
  • Operation Queues
  • Modern Concurrency in Swift

Cons of Manual Thread Creation

  • Responsibility to manage threads with system Conditions
  • Deallocaiton once they have finished execution
  • Improper Management cause memory leakage
  • Autorelease pool didnot manage threads created by us
  • Maintaining the order of execution

NOTE : so it’s not recommended to deal with Thread Api directly Instead there are some Apis by Apple which takes care of all stuffs

Grand Central Dispatch ( Dispatch ) : is a framework. which execute code concurrently on multicore hardware by submitting work to dispatch queues managed by the system

GCD is a queue based API that allows to execute closures on workers pool in FIFO order

Task Queue represents from where tasks submit in a thread pool and after its execution and submitted to Completed Tasks .

so here GCD comes into picture, it decides which thread used to execute the task , we just need to submit a pool of tasks to GCD , GCD simply manages a collection of dispatch queues. They are usually referred as queues. work submitted to these dispatch queues is executed on a pool of threads.

Dispatch Queue : Abstraction layer on the top of queue either work serially or concurrently but in FIFO order , An application can submit a task to queue in the forms of blocks or closures either synchronously or asynchronously.

Synchronous vs Asynchronous

Synchronous : Block the execution till current task is completed .

Asynchronous : Continue the execution of current task , while new task execute async.

Serial Queue vs Concurrent queue

  • Serial — one task at a time
  • Concurrent : multiple task at a time when it submitted in one go but they are dequeing seriallyImproper Management
  • Main Queue is also a serial queue

NOTE: This is just 30% of Concurrency will attach here more links once I’m done from my end so keep waiting :)

you can contact / follow me on twitter & linkedIn accounts.

Thanks for reading…

If you like my articles, please follow me. You can also support me by buying me a coffee.

--

--