Swift Concurrency
Tags: multithreading, threading
Published: 2024-07-09 05:40:23

## What is Concurrency?
### In Real Life:
Concurrency in real life refers to the ability to handle multiple activities simultaneously within a given timeframe. This can be seen in various scenarios where multiple tasks are being managed simultaneously.
### Examples:
* **Chef in a Kitchen:** A chef might prepare several dishes simultaneously. While one dish is baking in the oven, the chef can chop vegetables for another dish. This is the concurrent execution of tasks.
* **Office Work:** An employee might be handling emails, answering phone calls, and attending daily meetings. These tasks overlap in time, demonstrating concurrency.
* **Traffic Management:** Traffic lights at an intersection manage the flow of cars from different directions. While cars move in one direction, others wait, and pedestrians might cross the street, all managed concurrently.

### In Programming:
 In programming, concurrency refers to the execution of multiple tasks or processes simultaneously, potentially overlapping in execution time. It is a way to structure a program to do multiple things at once, improving efficiency and performance.

### Examples:
* **Multithreading:** Running multiple threads within a single application. Each thread can perform a different task, such as fetching data from the network, processing user input, and updating the user interface.
* **Asynchronous Programming:** Using constructs like async/await to perform non-blocking operations. For instance, fetching data from a server while allowing the user interface to remain responsive.
* **Parallel Processing:** Executing multiple processes or computations simultaneously, often on different CPU cores. This is common in scientific computing, image processing, and machine learning tasks.


![Concurrency](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/barirmb4jqbrpif2llnl.png)



## What is Parallelism?
### In Real Life:
 Parallelism in real life refers to the simultaneous execution of multiple tasks by multiple resources. It involves doing multiple things at the same time.
### Examples:
* **Construction Site:** Workers on a construction site performing different tasks simultaneously, such as one worker laying bricks while another installs windows.
* **Orchestra:** Different musicians play their instruments at the same time to create a symphony.
* **Restaurant:** Multiple chefs in a restaurant kitchen cooking different dishes at the same time, each using different equipment

### Programming:
 In programming, parallelism refers to the simultaneous execution of multiple computations or processes. It typically involves dividing a task into subtasks that can be processed concurrently on multiple processors or cores.
### Examples:
* **Parallel Processing:** Running multiple computations simultaneously on different CPU cores to speed up processing time.
* **Parallel Algorithms:** Algorithms that divide a problem into subproblems that can be solved concurrently, like matrix multiplication or sorting algorithms.
* **Distributed Systems:** Multiple servers processing different parts of a task at the same time to handle large-scale computations, such as in data centres or cloud computing
![Parallelism](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlgbr0qoarjoxma6gjb9.png)

## Concurrency vs Parallelism?

![Concurrency vs Parallelism](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uy8nk4pgx99x4nmdcbh.png)


**Concurrency:** Two queues share one vending machine, serving one person at a time by alternating between queues.
**Parallelism:** Each queue has its vending machine, allowing both queues to be served simultaneously.
> Parallelism Will give more speed but it involves more CPU resources.
 
> Because Parallelism involves more resources we need to find a way that Concurrency can be achieved.

## How Can We Achieve Concurrency in iOS?
1. [Achieving multi-threading by creating threads manually](https://dev.to/vinaykumar0339/achieving-multi-threading-by-creating-threads-manually-in-swift-2b3n)
2. [GCD (Grand Central Dispatch)](https://dev.to/vinaykumar0339/introduction-to-gcd-grand-central-dispatch-16b9)
3. Operation Queues
4. Modern Concurrency By Swift. (async and await)