This article focusses on dealing how to get more than one lock at a time if a multithread program is given along with avoiding the deadlocks.
Multithread programs – Due to the threads that keep on attempting to get multiple locks at once, these are very much prone to deadlocks. Understanding it with an example – a lock is already been acquired by a thread and then a second lock is attempted by the block then in that case, the program can freeze as the thread can potentially block the progress of other threads.
- Enforcement of ordering rule
- Assigning each lock in a unique manner to the program.
- Only allowing multiple locks to be acquired in ascending order.
Code #1 : Implementing the solution uisng a context manager.
Locks are acquired in the normal way using the context manager and for performing this task
acquire() function is used as there was more than one lock as shown in the code below :
Code #2 :
- Even after the acquisition of the locks specification in a different order in each function – the program will run forever without deadlock.
- Sorting the locks plays an important role according to the object identifier as locks after being sorted get acquired in a consistent manner regardless of how the user might have provided them to acquire().
- If multiple threads are nested as shown in the code below, to solve a subtle problem with detection potential deadlock, thread-local storage is used.
Code #3 :
On running this version of the program, one of the threads will crash with an exception such as :
Exception in thread Thread-1: Traceback (most recent call last): File "/usr/HP/lib/python3.3/threading.py", line 639, in _bootstrap_inner self.run() File "/usr/HP/lib/python3.3/threading.py", line 596, in run self._target(*self._args, **self._kwargs) File "deadlock.py", line 49, in thread_1 with acquire(y_lock): File "/usr/HP/lib/python3.3/contextlib.py", line 48, in __enter__ return next(self.gen) File "deadlock.py", line 17, in acquire raise RuntimeError("Lock Order Violation") RuntimeError: Lock Order Violation
Each thread remembers the lock been already acquired that’s why it’s been showing this error. The ordering constraints that acquired the locks is also enforced and list of previously acquired locks is checked by the
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Python vs PHP
- zip() in Python
- SHA in Python
- set add() in python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.