Overview
Scenarios are pre-configured simulations that demonstrate specific synchronization concepts. Each scenario sets up threads with instruction sequences and creates the shared resources they interact with.Scenario Structure
All scenarios follow a consistent pattern:- Create synchronization primitives (mutex, semaphore, etc.)
- Initialize shared state (account balance, resource counts, etc.)
- Create threads with specific instruction sequences
- Register threads with the engine
- Return context object containing all shared resources
Available Scenarios
The simulator includes seven scenarios demonstrating different synchronization concepts:Bank
Concept: Mutual exclusion with mutexFile:
bankScenario.jsDemonstrates: Critical section protection, race conditions, account consistencyPrinter
Concept: Resource pooling with semaphoreFile:
printerScenario.jsDemonstrates: Counting semaphore, limited resource allocation, job queuingRestaurant
Concept: Producer-consumer with condition variableFile:
restaurantScenario.jsDemonstrates: Wait/signal pattern, blocking and waking threadsLibrary
Concept: Readers-writers with monitorFile:
libraryScenario.jsDemonstrates: Concurrent reads, exclusive writes, monitor patternRace
Concept: Synchronization barrierFile:
raceBarrierScenario.jsDemonstrates: Barrier synchronization, checkpoint waitingHouse
Concept: Thread join and awaitFile:
houseScenario.jsDemonstrates: Task dependencies, join/await operationsPeterson
Concept: Peterson’s mutual exclusion algorithmFile:
petersonScenario.jsDemonstrates: Software-only mutual exclusion for two threadsScenario Deep Dive
Bank Scenario
Demonstrates mutex-based protection of a shared bank account. Source Code:bankScenario.js
threadCount- Number of client threadsoperations- Array of operations (WITHDRAW/DEPOSIT with amounts)initialBalance- Starting account balance
Thread Instruction Pattern
Thread Instruction Pattern
Printer Scenario
Demonstrates semaphore-based resource pooling with multiple printers. Source Code:printerScenario.js
jobCount- Number of print jobsprinterCount- Number of available printersjobs- Array of job configurations withpages
The semaphore capacity matches the number of printers, limiting concurrent access to available resources.
Restaurant Scenario
Demonstrates producer-consumer pattern with condition variables. Source Code:restaurantScenario.js
customerCount- Number of customer threadsmealsToCook- Number of meals the chef will prepare
Thread Patterns
Thread Patterns
Customer Thread:Chef Thread:
Library Scenario
Demonstrates readers-writers problem with monitor pattern. Source Code:libraryScenario.js
readerCount- Number of student reader threadswriterUpdates- Number of catalog updates by librarian
Multiple readers can access the library simultaneously, but writers require exclusive access.
Race Barrier Scenario
Demonstrates synchronization barrier at a checkpoint. Source Code:raceBarrierScenario.js
racerCount- Number of racers (must all reach checkpoint before any continue)
House Scenario
Demonstrates thread join and await for task dependencies. Source Code:houseScenario.js
durations- Object with duration for each stage (foundation, walls, roof, installations)
This scenario demonstrates how complex task dependencies can be modeled using JOIN_THREAD and AWAIT_ALL instructions.
Peterson Scenario
Demonstrates Peterson’s mutual exclusion algorithm for two threads. Source Code:petersonScenario.js
cyclesPerRobot- Number of times each robot uses the shared station
Configuring Scenarios
Scenarios accept configuration parameters that control:Thread Counts
Thread Counts
threadCount(Bank)jobCount(Printer)customerCount(Restaurant)readerCount(Library)racerCount(Race)- Fixed at 5 workers + 1 architect (House)
- Fixed at 2 robots (Peterson)
Resource Limits
Resource Limits
initialBalance(Bank)printerCount(Printer)mealsToCook(Restaurant)- No explicit limit (Library - monitor handles coordination)
totalRacers(Race - for barrier size)
Operation Details
Operation Details
operationsarray with type and amount (Bank)jobsarray with pages (Printer)writerUpdates(Library)durationsobject for stages (House)cyclesPerRobot(Peterson)
Scenario-Algorithm Relationship
Each scenario demonstrates a specific synchronization algorithm:| Scenario | Algorithm | Key Concept |
|---|---|---|
| Bank | Mutex | Mutual exclusion with locks |
| Printer | Counting Semaphore | Resource pool management |
| Restaurant | Condition Variable | Producer-consumer coordination |
| Library | Monitor (Readers-Writers) | Concurrent reads, exclusive writes |
| Race | Barrier | Synchronization points |
| House | Join/Await | Task dependency graphs |
| Peterson | Peterson’s Algorithm | Software-only mutual exclusion |
Creating Custom Scenarios
To create a new scenario:Next Steps
Threads
Learn about the Thread model and states
Instruction Set
Explore all available instructions