#C13455. Concurrent Inventory System Simulation
Concurrent Inventory System Simulation
Concurrent Inventory System Simulation
This problem simulates a concurrent inventory system where multiple threads attempt to purchase items from a shared inventory. The inventory initially has S items and there are T threads. Each thread performs N purchase attempts. A purchase succeeds only if the current stock is positive. In such a case, the stock is decremented by one.
The correct final inventory is computed by the formula:
[ \text{remaining stock} = S - \min(S, T \times N) ]
You must implement proper concurrency control (e.g., using mutex/lock) to ensure that the inventory is not oversold in a concurrent environment. The program should read the input parameters from standard input, simulate the order placements concurrently, and finally output the final remaining stock.
inputFormat
The input is provided via standard input (stdin) and consists of three space-separated integers:
- S: the initial number of items in the inventory.
- T: the number of threads (or concurrent workers).
- N: the number of purchase attempts each thread makes.
For example: 50 10 100
outputFormat
The output, printed to standard output (stdout), is a single integer representing the remaining number of items in the inventory after all threads have finished placing orders.
For example: 0
50 10 100
0
</p>