Back to problems

Thread-Safe Buy/Sell Without Overselling

Object-Oriented Programming · Optiver · Medium

Requirements Build an inventory component with operations for purchasing and selling stock shares. When several threads invoke these operations at the same time, the implementation must never allow the position to become negative or otherwise violate its inventory limit. Protect the shared position state with synchronization, such as a mutex or lock. Use the following API: buy adds shares to the current position. sell should subtract shares only when enough inventory is…

Checking your access…