Mercurial
view asyncio_threads/bank_question/main.py @ 279:b3b547563ec7
Add Google connector service and agent wiki
Implement the C/Seobeo Google Drive and Gmail connector with encrypted OAuth storage, Zenbu authentication, browser testing, AI tool discovery, chunked HTTP decoding, and Bazel coverage. Consolidate repository guidance into progressive wiki documentation and enforce arena-first allocation for new first-party C code.
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 84c338fd-0939-4bb3-b7f3-1062eb213e5d
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 17 Aug 2026 22:22:36 -0700 |
| parents | 46daba6e3cf4 |
| children |
line wrap: on
line source
# # Bank Account # # You are given a simplified multi-threaded “bank account” implementation used by one of our infrastructure teams. The current code processes deposits concurrently but produces incorrect results. # # Task # # Identify the concurrency bugs in the implementation. # # Explain why these bugs occur. # # Fix the implementation without modifying the existing BankAccount class directly. # # You may use subclassing (Python) or embedding (Go-style composition), depending on the language you choose. # # Ensure your solution is thread-safe and produces the correct final balance (1200). from threading import Lock from concurrent.futures import ThreadPoolExecutor import time class BankAccount: def __init__(self, balance): self.balance = balance self._lock = Lock() def deposit(self, amount): while (self._lock.locked()): pass self._lock.acquire() new_balance = self.balance + amount # read time.sleep(0.1) # simulate delay self.balance = new_balance # write self._lock.release() account = BankAccount(0) with ThreadPoolExecutor(max_workers=2) as executor: futures = [ executor.submit(account.deposit, 500), executor.submit(account.deposit, 700), ] print(account.balance)