multithreadingdeadlock

how to guarantee no deadlock


I was stuck at one question in an interview.

Given two threads, and each one has a lock, how to guarantee no deadlock.

My knowledge is that it is not easy to avoid deadlock, that's why I got stuck. Can anybody give a hint.

Thanks!


Solution

  • The description is a bit lacking, but if you impose a locking order (e.g, if the locks are A and B, never lock B unless you already locked A, and never release A while B is locked), then deadlock won't occur.