Two Workers Lose One Inventory Update
A simulated warehouse has one shared integer count initially equal to ten. Two worker threads each record one returned item by reading the count, adding one locally and writing the new value. The teacher supplies an execution trace in which both workers read ten before either writes. The team sees eleven at the end and suspects that the arithmetic hardware is faulty. A proposed repair locks only the write instruction while leaving both reads outside the lock. The exercise concerns a classroom shared-memory model with ordinary non-atomic read and write steps, no database transaction and no automatic protection. Students must explain the trace and propose a synchronization boundary that protects the complete increment without claiming that every operation in the program needs one global lock.
1. Explain the final value and assess the proposed write-only lock.
Read the worked answer
Each worker independently computes eleven from the same read value ten. Both later write eleven, so one update is lost. Locking only each write still permits the two stale reads and does not protect the read-modify-write operation.
2. Propose a correct repair and a relevant limitation.
Read the worked answer
Use an appropriate atomic increment or hold the same mutex across reading, incrementing and writing. All cooperating accesses to that invariant must obey the design. A mutex protects this shared update but does not by itself guarantee fairness, absence of every deadlock or correctness of unrelated data.