Overview
The Bank Account scenario simulates multiple clients performing concurrent transactions (deposits and withdrawals) on a shared bank account. This demonstrates the classic race condition problem where unsynchronized access to shared resources can lead to incorrect balances.Real-World Problem
Imagine a bank account that multiple clients can access simultaneously:- Multiple ATMs processing transactions
- Online banking operations happening concurrently
- Mobile app transactions overlapping with web transactions
Shared Resources
The shared resource is a bank account object containing a
balance property that all client threads access and modify.Synchronization Algorithm
This scenario uses a Mutex to ensure that only one client can access the account at a time:Perform Transaction
Client safely reads the balance, performs the operation (deposit/withdrawal), and updates the balance
Scenario Setup
bankScenario.js
Configuration Options
| Parameter | Description | Default |
|---|---|---|
threadCount | Number of client threads | Varies |
operations | Array of operations (type + amount) | Withdrawals of 100 |
initialBalance | Starting account balance | Varies |
Example Execution Flow
Thread Instructions
Each client thread executes the following instruction sequence:- ACQUIRE - Request mutex lock
- WITHDRAW or DEPOSIT - Perform the transaction with specified amount
- RELEASE - Release mutex lock
- END - Terminate thread
The mutex ensures mutual exclusion: while one client holds the lock, all other clients must wait, preventing race conditions and ensuring account balance consistency.
Key Learning Points
- Critical Section: The code between ACQUIRE and RELEASE is the critical section
- Race Condition: Without the mutex, concurrent operations lead to incorrect balances
- Mutual Exclusion: Only one thread can hold the mutex at any time
- Atomicity: Each transaction appears to execute atomically from other threads’ perspective