A multithreaded C++ simulation demonstrating how concurrent processes safely interact with shared resources using mutexes and condition variables, modeled through a scenario of students sharing a drink barrel monitored by a bartender.
This simulation consists of several interacting components:
- Student Threads – Each student runs in its own thread and repeatedly attempts to access the shared barrel, consume a random number of drinks, and then enter a thinking state.
- Bartender Thread – Remains idle until notified that the barrel is empty, then refills it a limited number of times.
- Shared Barrel – A global resource protected by mutex locks to prevent race conditions during access.
- Control System – Allows the simulation to be paused and resumed while displaying Process Control Block (PCB) data for all active threads.
Together, these components demonstrate real-world thread behavior such as contention, waiting, signalling, and synchronized resource management.
- Each student is a thread.
- Students attempt to take a random number of drinks (1–5).
- After drinking, they enter a "thinking" state for a random period.
- They maintain a PCB (Process Control Block) showing:
- Thread ID
- State
- Drinks requested
- Drinks consumed
- Wait count
- Sleeps until the barrel is empty.
- Refills the barrel 3 times maximum.
- Each refill adds 31–50 drinks.
- Displays its wake count and current state.
- Shared resource initialized with 50 drinks.
- Access controlled via mutex locks.
- When empty, it signals the bartender using a condition variable.
- Pressing ENTER toggles the simulation between paused and running states.
- While paused, all threads wait.
- PCB data for all entities is printed.
- Open a terminal window
cdto the project directory- Build command:
g++ -pthread Project.cpp –o Project - Run command:
./Project - Pause command: Enter
- Resume command: Enter
- Exit command: Ctrl+c