Thread
A thread is the smallest unit of execution within a process. It represents a single sequence of instructions that can be scheduled and executed independently by the operating system’s scheduler.
Multithreading
The term “multithreading” refers to the use of multiple threads within a single process. Multithreading allows different parts of a program to execute concurrently and share resources such as memory and I/O devices.
Parallelism
Threads enable parallelism by executing multiple tasks simultaneously on multicore processors. By utilizing multiple threads, applications can take advantage of the available CPU cores to improve performance and responsiveness.
Thread Safety
When multiple threads access shared resources concurrently, it’s essential to ensure thread safety to avoid race conditions and data corruption. Synchronization mechanisms such as locks, semaphores, and atomic operations are used to coordinate access to shared resources and prevent conflicts between threads.
Sync
In synchronous programming, tasks are executed sequentially, one after the other. Each task must wait for the previous one to complete before it can start. Synchronous operations block the execution of the program until they are finished, meaning that the program waits for the operation to complete before moving on to the next task.
Async
In asynchronous programming, tasks can be executed concurrently or in parallel, allowing the program to continue executing other tasks while waiting for certain operations to complete. Asynchronous operations do not block the execution of the program. Instead, they execute in the background, and the program can continue performing other tasks while waiting for the asynchronous operation to finish.
Race Condition
A race condition occurs when two tasks are executed concurrently, when they should be executed sequentially in order to be done correctly. You cant change view constraint while it is being calculated. So UI activity should be done in main thread so it is executed sequentially.
To achieve concurrency in iOS there are 2 build in APIs available
- GCD
- NSOPERATIONQUEUE