Overview
The Restaurant scenario simulates a chef preparing meals for customers who wait until their food is ready. This demonstrates the classic producer-consumer pattern using condition variables for efficient waiting and notification.Real-World Problem
Imagine a restaurant where:- Customers arrive and wait for their meals
- A chef prepares dishes one at a time
- Customers should sleep (not busy-wait) until food is ready
- The chef signals customers when each dish is prepared
Shared Resources
The shared resource is a restaurant state object containing:
availableDishes- Number of dishes ready to servetotalCooked- Total dishes prepared by cheftotalEaten- Total dishes consumed by customersfoodCondition- Condition variable for synchronization
Synchronization Algorithm
This scenario uses a Condition Variable for producer-consumer coordination:Customers Wait
Customer threads execute WAIT on the condition variable, blocking until food becomes available
Scenario Setup
restaurantScenario.js
Configuration Options
| Parameter | Description | Default |
|---|---|---|
customerCount | Number of customer threads | Minimum 1 |
mealsToCook | Number of meals the chef prepares | Minimum 1 |
Example Execution Flow
Thread Instructions
Customer threads:- WAIT_FOOD - Wait on condition variable until food is available
- EAT_FOOD - Consume a dish (decrement availableDishes)
- END - Terminate
- COOK_DISH - Prepare one dish (increment availableDishes)
- SIGNAL_FOOD - Wake up one waiting customer
- Repeat for each meal
- END - Terminate
Condition variables enable efficient waiting. Instead of busy-waiting (repeatedly checking if food is ready), customers sleep until explicitly woken by the chef’s signal.
Producer-Consumer Pattern
This scenario demonstrates the classic pattern:| Role | Thread | Action |
|---|---|---|
| Producer | Chef | Creates resources (dishes) |
| Consumer | Customers | Consumes resources (eats) |
| Coordination | Condition Variable | Signals when resources available |
Key Learning Points
- Condition Variables: Enable threads to wait efficiently for conditions
- Producer-Consumer: Chef produces, customers consume
- Signal/Wait: Chef signals availability, customers wait for signal
- Starvation: If meals < customers, some customers never eat
- Efficiency: No busy-waiting; threads block until notified