Multithreading in Python | Threads & Process | Dead-Lock & Race Condition | Lock Variable
Tags: python, multithreading, deadlock, lockvariable
Published: 2023-08-17 20:25:33

## **Processes and Threads:**
A 'Process' is a program in execution, whereas a 'Thread' is a separate execution path in a 'Process'. A 'Thread' can be called as a 'Light-Weight Process' as all the threads within the process share the same computing resources like memory, processing power, etc.

Examples :

1. Google Chrome -> 'Source Code + Deployment Set-Up' is the program in this case, 'Running application' is the process and different tabs can be considered as threads.
2. MS Word -> Here, the different operations like 'Adding text', 'Adding Media', 'Inserting Links', etc can be considered as Threads within the process(MS Word).

## **Multithreading:**

- It is the mechanism of running/executing multiple threads within the same process.
- <u>Module in Python</u>: threading
- 'threading' is already available in the installed python -versions. Just import the module and you are good to go!

### **Tutorial:**

```python
# Import the threading module to work with threads
import threading

# Define a function that will be executed by each thread
def test(x):
    print("Hello", x)

# Create a list of threads, each targeting the 'test' function with different arguments
threads = [threading.Thread(target=test, args=(i,)) for i in ['a', 'b', 'c']]

# Start each thread in the list
for i in threads:
    i.start()
```
**Output:**
```Output
Hello a
Hello b
Hello c
```
## **Mutual Exclusion - Lock (or) Mutex Variable:**

```python
# Import the required modules
import time        # Import the time module for sleep function
import threading   # Import the threading module for creating and managing threads

# Define a function to be run in the threads
def test1(x):
    for i in range(3):
        print("{} iteration for arg {}".format(i, x))
        time.sleep(1)  # Sleep for 1 second to simulate some work

# Create a list of threads
threads = [threading.Thread(target=test1, args=(i,)) for i in range(1, 4)]

# Start each thread
for thread in threads:
    thread.start()
```
**Output:**
```Output
0 iteration for arg 1
0 iteration for arg 2
0 iteration for arg 3
1 iteration for arg 1
1 iteration for arg 2
1 iteration for arg 3
2 iteration for arg 1
2 iteration for arg 2
2 iteration for arg 3
```
In this case, the first running thread sleeps for 1 second after the print function in the 1st iteration, then the second thread executes its 1st iteration and sleeps, and the same continues. This occurs due to the fact that all the threads within the same process share the same resources and whenever a thread sleeps the other comes into play.

To avoid this we use **Lock-Variable**. By using this we achieve **Mutual-Exclusion**.

```python
import time        # Import the time module for sleep function
import threading   # Import the threading module for creating and managing threads

# Define a function to run in the threads
def test1(x):
    for i in range(3):
        print("{} iteration for arg {}".format(i, x))
        time.sleep(1)  # Sleep for 1 second to simulate some work

# Create a list of threads
threads = [threading.Thread(target=test1, args=(i,)) for i in range(1, 4)]

# Create a thread lock to synchronize thread execution
lock = threading.Lock()

# Start each thread while holding the lock to prevent interleaving
for thread in threads:
    with lock:  # Acquire the lock before starting the thread
        thread.start()
```
In this code, a thread lock is introduced to address the issue of output interleaving that was present in the previous code. The lock ensures that only one thread can execute its code within the context of the lock at a time.

This approach is better than the previous code because it adds synchronization. Without synchronization, the threads could interleave their output messages and cause confusion.
By using a thread lock, the "start" method of each thread is wrapped in a lock context, ensuring that each thread starts execution one after the other, without overlapping output.
This results in a clearer and more organized output that reflects the intended behaviour.

**Output:**
```Output
0 iteration for arg 1
1 iteration for arg 1
2 iteration for arg 1
0 iteration for arg 2
1 iteration for arg 2
2 iteration for arg 2
0 iteration for arg 3
1 iteration for arg 3
2 iteration for arg 3
```

## **Dead-Lock & Race Condition:**
**Dead-Lock** : It is a condition when a process A having access to resource i and waiting for access to resource j is in loop with a process B which has the access locked to resource j and waiting for resource i.
![DEAD-LOCK](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yc9b0ynzlmt7tfo10wsr.png)

**Race Condition** : It is a condition that appears when two or more threads sharing access to the resources and variables proceeds to their executions according to their schedule but it might lead to overwriting of variables leading to unexpected outcomes.

## **Pros & Cons of Multithreading:**

**Pros**:
- Multiple tasks can be done simultaneously.
- Resource Sharing.
- Efficient utilization of memory and processors.
- Overall performance improvements.

**Cons**:
- Synchronization is complex.
- Debugging is difficult.
- Memory and resources issues like deadlock, etc.

## **Some Useful Functions from this module:**

1. activeCount() : Returns the number of active threads.
2. currentThread() : Returns the currently running thread.
3. enumerate() : Returns a list of threads(active and dummy).
4.  run() : It is the entry point for the thread of a process.
5. start() : It starts the execution of the thread.
6. join() : It waits for the complete execution of the thread before moving to the execution of the next instruction in the main program.
7. isAlive() : It checks if the thread is alive / running or completed.

---

Resources : [_**Official Documentation**_](https://docs.python.org/3/library/threading.html)

Comments in the code blocks are added using ChatGPT.

Do Upvote, Follow and Subscribe if you've learnt something from this post!

---

**_Linkedin:_** <div class="badge-base LI-profile-badge" data-locale="en_US" data-size="large" data-theme="dark" data-type="HORIZONTAL" data-vanity="mubeen-zaki" data-version="v1"><a class="badge-base__link LI-simple-link" href="https://in.linkedin.com/in/mubeen-zaki?trk=profile-badge">Md Mubeen Ali Zaki</a></div>