Getting Stuck: The Deadlock That Ate My Program In this installment of Getting Stuck , I set out to do something totally ordinary: write a tiny banking system where two accounts can transfer money to each other. Easy enough, right? Just some locks and arithmetic. Instead, I ended up with a program that sometimes worked, sometimes froze solid, and once even locked itself in an eternal staring contest with my CPU. This is the story of my brush with deadlock . Step 1: The Setup I wanted a simple class for accounts: import threading class Account: def __init__(self, balance): self.balance = balance self.lock = threading.Lock() def withdraw(self, amount): self.balance -= amount def deposit(self, amount): self.balance += amount Then a function to transfer between accounts: def transfer(src, dst, amount): src.lock.acquire() dst.lock.acquire() src.withdraw(amount) dst.deposit(amount) src.lock.release() dst...
A curious mind exploring the beautiful world