Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 807
skydoor Avatar asked Feb 17 '10 21:02

skydoor


People also ask

How can avoid deadlock in SQL Server?

Useful ways to avoid and minimize SQL Server deadlocksTry to keep transactions short; this will avoid holding locks in a transaction for a long period of time. Access objects in a similar logical manner in multiple transactions. Create a covering index to reduce the possibility of a deadlock.


1 Answers

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.

like image 90
Ofir Avatar answered Oct 15 '22 21:10

Ofir