A Series-A fintech, motto 'move fast'. Money moves fast too: under concurrent transfers the balance sometimes adds up, sometimes doesn't. The CTO suggested 'adding sleep(10ms) so the threads don't collide'. The regulator would suggest something else. Make account operations actually atomic: a mutex and RAII locks.
Implement a BankAccount that is safe to use from multiple threads. Operations on a closed account must be rejected.
Adapted from exercism/cpp (MIT).
std::mutexopen() resets the balance to 0; close() makes the account unusablebalance(), deposit(), withdraw() on a closed account must throwdeposit/withdraw reject negative amounts; withdraw rejects overdraftsbalance() also be protected by the mutex?deposit without a lock?std::lock_guard help keep the critical section correct under exceptions?Guard balance and open-state with a std::mutex locked via lock_guard in every method.
Throw from deposit/withdraw/balance when the account is closed or the amount is invalid.
Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.