commit 6b0c1e8be8b0b11ed0b2805fb7d227523e2f0cae Author: Nathan Date: Fri Nov 24 18:55:11 2023 +0100 test diff --git a/pylock.py b/pylock.py new file mode 100644 index 0000000..c94586a --- /dev/null +++ b/pylock.py @@ -0,0 +1,33 @@ +from threading import Thread, Lock + +class atm: + def __init__(self): + self.balance = 1000 + self.lock = Lock() + + def takeMoneyOut(self, quantity): + with self.lock: + if self.balance >= quantity: + self.balance -= quantity + return True + else: + return False + +def user(atm, amount): + if atm.takeMoneyOut(amount): + print("Taken out: ", amount) + else: + print("Not enough balance") + +theAtm = atm() +users = [] + +for i in range(10): + t = Thread(target=user, args=(theAtm, 200)) + users.append(t) + t.start() + +for t in users: + t.join() + +print("Final balance: ", theAtm.balance)