Concurrent restaurant order + pickup simulator
Design and implement a thread-safe simulation for a restaurant that:
-
Receives incoming orders concurrently (multiple producer threads).
-
Prepares orders concurrently (one or more worker/chef threads).
-
Allows customers to pick up orders concurrently (multiple consumer threads).
Requirements
-
Each order has:
-
order_id
(unique integer)
-
item_name
(string)
-
cook_time_ms
(positive integer)
-
API / operations to support (names can vary):
-
placeOrder(order_id, item_name, cook_time_ms)
-
startKitchen(num_chefs)
(starts background processing)
-
pickupOrder(order_id)
blocks until the specified order is ready, then returns it
-
shutdown()
stops background threads gracefully
-
Correctness constraints:
-
No order can be picked up before it is cooked.
-
Each order is cooked
exactly once
and picked up
at most once
.
-
No deadlocks; avoid busy-waiting.
-
The system should handle
N
orders with concurrent placement and pickup.
What to discuss/implement
-
Your choice of synchronization primitives (mutex/lock, condition variable, semaphore, blocking queue, etc.).
-
Data structures used to track order state.
-
How you ensure fairness and avoid race conditions.
(You may assume a single process and shared memory; no networking required.)