Assignment 1 involves implementing a Marketplace that models the Multiple Producers - Multiple Consumers problem:
- Producers offer products for sale
- Consumers purchase available products
- Consumers can become producers by removing products from their shopping cart
The implementation utilizes multiple execution threads accessing shared resources, addressing a problem in the domain of Parallel Computing and Multithreading.
- Models the consumer thread
- Uses a cart ID (represented as a list)
- Can add or remove products using a dictionary of functions
- Calls
place_order()
to empty the shopping cart when all operations are complete - Waits for a specified time if an operation fails
- Thread execution ends when product ordering is activated
- Models the producer thread
- Publishes products through the Marketplace
- Receives a list of products to iterate infinitely
- Waits for a specified time after successful publishing
- Waits for a creation-time specified duration if publishing fails
- Central point of implementation
- Acts as a facade for synchronizing and communicating between threads
- Provides methods called by both consumers and producers
- Maintains:
- A list of available products
- A dictionary mapping cart IDs to product lists
- A dictionary mapping products to their producers (used when consumers remove products from their cart)
- A dictionary tracking the maximum number of products a producer can publish
- Non-thread-safe operations (e.g., increments, decrements) are protected using Locks
- The
publish()
,add_to_cart()
, andremove_from_cart()
functions modify producer queue capacities
- When an order is placed, all products in the cart are displayed to the output stream
- The cart (modeled as a list) is then returned to the consumer