educative.io

Deadlock example is incorrect

The code for the deadlock example has a bug, the threads initiated are running the same function:

    t1 = Thread(target=thread_one, args=(lock1, lock2))
    t2 = Thread(target=thread_one, args=(lock1, lock2))

The reason there is an execution timeout is because the first thread which acquires the lock never releases it for the second thread to acquire. This is in opposition to the reason stated:

The above example demonstrates that a thread can’t release a lock it has not locked. Furthermore, trying to release an unacquired lock will result in an exception.

When fixed by having the second thread run the thread_two function, there are no errors and no deadlock.

1 Like

Hi @Evan1

My name is Shahrukh Naeem. I hope everything is going well with you.

We appreciate your keen interest, and we thank you for pointing this out. It is great seeing that our users are always coming up with betterments in the code. We’ll look into it, your feedback is much appreciated.

This is not an example for deadlock, as the code runs smooth and exits well.

But the article has NOT been updated in 2 years!