educative.io

Educative

I felt Reentrant lock would not led to deadlock

As I understand the RLock can be re-acquired but I don’t understand why the below code still goes in deadlock

from threading import *
import time

def thread_A(lock1, lock2):
    lock1.acquire()
    print("{0} acquired lock1".format(current_thread().getName()))
    time.sleep(1)
    lock2.acquire()
    print("{0} acquired both locks".format(current_thread().getName()))


def thread_B(lock1, lock2):
    lock2.acquire()
    print("{0} acquired lock2".format(current_thread().getName()))
    time.sleep(1)
    lock1.acquire()
    print("{0} acquired both locks".format(current_thread().getName()))


if __name__ == "__main__":

    lock1 = RLock()
    lock2 = RLock()

    Thread(target=thread_A, args=(lock1, lock2)).start()
    Thread(target=thread_B, args=(lock1, lock2)).start()

Hi @Vipul2

I am sorry I am unable to find the specific lesson in the given course. Can you please confirm the course name? Thank you.