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!
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!
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.
With a single lock it's not possible to get deadlocked unless one refuses to release their lock -- in which case the waiting thread is called starved. For multiple locks, they must be released in the reverse order that they were acquired, and both threads must agree on the order.
What you are trying to avoid here is this situation:
A has lock 1 waiting on lock 2
B has lock 2 waiting on lock 1
A lot of work on concurrent/parallel programming is focused on lock-free designs. Not an exact answer to your question, but as Andrew already mentioned on this thread, the best way to avoid deadlocks is to not lock at all. A lot of very smart people working on concurrency issues are of that opinion.
Lock ordering is preferable to timeouts/deadlock detection, but sometimes timeouts are necessary, particularly if you don't control all of the components in the system: hence deadlock timeout/detection in databases. If all of the callers were clever enough, deadlocks would never happen, but commonly not all of the callers are clever enough.
The answer your interviewers were going for is probably WaitForMultipleObjects(). That's a Windows API that locks both (or more) resources simultaneously.
There are known deadlock avoidance algorithms that can detect if a deadlock is about to occur and avoid the system getting into that state. For example, the Banker's Algorithm.