Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement a Stoppable Producer–Consumer System in C++
Implement a Stoppable Producer–Consumer System in C++ (Medium)
Design and implement a C++ system with three classes: Storage, Factory, and Consumer to manage a producer-consumer pattern with rate control and graceful shutdown.
Constraints & Assumptions
Storageis a fixed-capacity FIFO (First-In, First-Out) queue forintvalues.store(item): Attempts to insert an item. It is non-blocking and returnstrueon success. If the storage is full, it does nothing and returnsfalse.retrieve(result): Attempts to remove the oldest item. It is non-blocking and returnstrueon success, placing the item inresult. If the storage is empty, it does nothing and returnsfalse.- The system operates with one factory and one consumer running concurrently.
- A positive
itemsPerSecparameter controls the rate at which each worker attempts its operations. - The caller is responsible for initiating and managing the shutdown process, not a hardcoded timer within the worker threads.
Core Implementation Tasks:
- Thread-Safe Bounded Storage: Implement the
Storageclass with thread-safestoreandretrievemethods. Explain critical sections, atomicity, and why separate checks foremptyorfullbefore mutation are unsafe. Ensure exception-safe locking and FIFO semantics. - Rate-Controlled Workers and Shutdown: Implement
FactoryandConsumerclasses. The factory produces sequential integers (0, 1, 2, ...). Ifstorefails due to a full queue, the item is dropped, and the next attempt uses the next integer. The consumer attempts one retrieval per rate-controlled period. Implement a mechanism for the caller to request shutdown and ensure worker threads are stopped and joined promptly. - Memory Ordering and Lock-Free Extension: Discuss the implications of memory ordering for the shutdown flag (why a plain
boolis insufficient). Explain when relaxed atomic ordering is acceptable and when acquire/release semantics are necessary. Explore how a fixed-size, single-producer/single-consumer (SPSC) ring buffer could replace the mutex-based queue for theStoragecomponent. Analyze the data-race and happens-before implications, safe publication of writes, and safe reuse of slots. Discuss why this SPSC design doesn't directly generalize to multiple producers or consumers.
AI interview prep
Core dilemma
Balancing thread-safe data structure integrity and performance with responsive, graceful shutdown under rate-controlled concurrent operations.
Mid-level signals3
Senior signals4
Red flags5
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a Stoppable Producer–Consumer System in C++
Implement a Stoppable Producer–Consumer System in C++ (Medium)
Design and implement a C++ system with three classes: Storage, Factory, and Consumer to manage a producer-consumer pattern with rate control and graceful shutdown.
Constraints & Assumptions
Storageis a fixed-capacity FIFO (First-In, First-Out) queue forintvalues.store(item): Attempts to insert an item. It is non-blocking and returnstrueon success. If the storage is full, it does nothing and returnsfalse.retrieve(result): Attempts to remove the oldest item. It is non-blocking and returnstrueon success, placing the item inresult. If the storage is empty, it does nothing and returnsfalse.- The system operates with one factory and one consumer running concurrently.
- A positive
itemsPerSecparameter controls the rate at which each worker attempts its operations. - The caller is responsible for initiating and managing the shutdown process, not a hardcoded timer within the worker threads.
Core Implementation Tasks:
- Thread-Safe Bounded Storage: Implement the
Storageclass with thread-safestoreandretrievemethods. Explain critical sections, atomicity, and why separate checks foremptyorfullbefore mutation are unsafe. Ensure exception-safe locking and FIFO semantics. - Rate-Controlled Workers and Shutdown: Implement
FactoryandConsumerclasses. The factory produces sequential integers (0, 1, 2, ...). Ifstorefails due to a full queue, the item is dropped, and the next attempt uses the next integer. The consumer attempts one retrieval per rate-controlled period. Implement a mechanism for the caller to request shutdown and ensure worker threads are stopped and joined promptly. - Memory Ordering and Lock-Free Extension: Discuss the implications of memory ordering for the shutdown flag (why a plain
boolis insufficient). Explain when relaxed atomic ordering is acceptable and when acquire/release semantics are necessary. Explore how a fixed-size, single-producer/single-consumer (SPSC) ring buffer could replace the mutex-based queue for theStoragecomponent. Analyze the data-race and happens-before implications, safe publication of writes, and safe reuse of slots. Discuss why this SPSC design doesn't directly generalize to multiple producers or consumers.