This question from elements of programming interviews claims there is a deadlock in the following code in the scenario where "U1 initiates a transfer to U2 and immediately afterwards U2 initiates a transfer to U1. Since each transfer takes place in a separate thread its possible for the first thread to lock U1 and then the second to lock U2"
class Account:
_global_id = 0
def __init__(self, balance):
self._balance = balance
self._id = Account._global_id
Account._global_id += 1
self._lock = threading.RLock()
def get_balance(self):
return self._balance
@staticmethod
def transfer(acc_from, acc_to, amount):
th = threading.Thread(target=acc_from._move, args=(acc_to, amount))
th.start()
def _move(self, acc_to, amount):
with self._lock:
if amount > self._balance:
return False
acc_to._balance += amount
self._balance -= amount
print('returning True')
return True
I do not see how there is a deadlock in that case. U1 and U2 have separate locks and as far as I can tell, thread1 just locks U1 and thread2 locks U2 since the _move method just uses self._lock and doesn't touch acc_to._lock. What am I missing?
acc_to._balance += amount
was guarded by awith acc_to._lock
statement, but I'd be curious if we're missing something.