Implement a thread-safe Bank:
bank = Bank([100, 50, 0]) # balances of accounts 0, 1, 2
bank.transfer(0, 2, 30) # True: moved 30 from account 0 to 2
bank.transfer(1, 0, 80) # False: account 1 only has 50, nothing changes
bank.balance(2) # 30
Rules:
transfer(src, dst, amount)returnsFalse(and changes nothing) ifsrc == dst, ifamount <= 0, or ifsrchas less thanamount.- Use one lock per account, not one global lock. Transfers between unrelated accounts must be able to run at the same time.
- It must never deadlock, even when
a → bandb → atransfers run concurrently. Bank(balances, work=None): ifworkis given, callwork()while holding both locks during a successful transfer. The tests use it to simulate slow operations.
Constraints: src, dst and the account passed to balance are always valid account indices; balances and amounts are non-negative integers.
Show hint
A deadlock needs two transfers that each hold one lock and wait for the other's. If every transfer took its two locks in the same agreed order, could that happen?