College assignment requested that we modify the code below to stop it from deadlocking, without changing the main function.
Right now, it is deadlocking because of how the locks end up waiting for each other. My professor mentioned using os.fork, which is not possible as I am using a Windows machine.
import threading
x = 0
def task(lock1, lock2, count):
global x
for i in range(count):
lock1.acquire()
lock2.acquire()
# Assume that a thread can update the x value
# only after both locks have been acquired.
x+=1
print(x)
lock2.release()
lock1.release()
# Do not modify the main method
def main():
global x
count = 1000
lock1 = threading.Lock()
lock2 = threading.Lock()
T1 = threading.Thread(target = task, args = (lock1, lock2, count))
T2 = threading.Thread(target = task, args = (lock2, lock1, count))
T1.start()
T2.start()
T1.join()
T2.join()
print(f"x = {x}")
main()
1 2 4 3 5 7 8 9 6....