Fix and harden an object pool
Refactor a Buggy C++ Object Pool into a Correct, Thread-Safe Template
You are tasked with refactoring an existing, buggy C++ object pool. The current implementation suffers from critical issues including race conditions, double-free vulnerabilities, ABA problems, and resource leaks during concurrent borrow/return operations. Your goal is to transform it into a robust, generic template that guarantees correctness under high concurrency and ensures safe shutdown.
Constraints & Assumptions
- Language Standard: C++17 or later.
Requirements
- Generic Template: Implement a generic, thread-safe object pool template for objects of type
T. - Borrow Operations:
- Provide a
borrow()method that blocks when the pool is empty and creates new objects up to a predefined maximum capacity. - Provide an overloaded
borrow_for(duration)method with a timeout. - Ensure fairness (FIFO) among waiting threads and prevent spurious wakeups.
- Provide a
- Return Operations:
- Implement
returnObject(obj)to safely return an object to the pool. - The method must reject invalid returns (e.g., objects not originating from this pool) and duplicate returns of the same object.
- Support optional object resetting before reuse via a configurable functor.
- Implement
- RAII Handle:
- Enforce Resource Acquisition Is Initialization (RAII) using a scoped handle. This handle must automatically return the object to the pool when it is destroyed.
- The handle must be movable but not copyable.
- Safe Shutdown:
- The pool's destructor must safely stop any ongoing operations, wake up all waiting threads, and release all acquired resources without introducing deadlocks.
- Concurrency Primitives:
- Avoid busy-waiting. Correctly utilize
std::mutex,std::condition_variable, andstd::atomic.
- Avoid busy-waiting. Correctly utilize
- ABA Problem Prevention:
- Implement mechanisms to prevent ABA issues, such as using generation counters per item.
- Testing:
- Include minimal unit tests demonstrating correctness under multiple threads.
- Documentation:
- Document the time complexity of all core operations.
Deliverables
- A single, self-contained C++17 implementation of the object pool.
- Minimal unit tests.
- Brief notes on time complexity.
AI interview prep
Core dilemma
Balancing high-performance concurrent access to shared resources with absolute correctness, thread-safety, and robust error handling (e.g., ABA, double-free, leaks) under all operational conditions, including graceful shutdown.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Fix and harden an object pool
Refactor a Buggy C++ Object Pool into a Correct, Thread-Safe Template
You are tasked with refactoring an existing, buggy C++ object pool. The current implementation suffers from critical issues including race conditions, double-free vulnerabilities, ABA problems, and resource leaks during concurrent borrow/return operations. Your goal is to transform it into a robust, generic template that guarantees correctness under high concurrency and ensures safe shutdown.
Constraints & Assumptions
- Language Standard: C++17 or later.
Requirements
- Generic Template: Implement a generic, thread-safe object pool template for objects of type
T. - Borrow Operations:
- Provide a
borrow()method that blocks when the pool is empty and creates new objects up to a predefined maximum capacity. - Provide an overloaded
borrow_for(duration)method with a timeout. - Ensure fairness (FIFO) among waiting threads and prevent spurious wakeups.
- Provide a
- Return Operations:
- Implement
returnObject(obj)to safely return an object to the pool. - The method must reject invalid returns (e.g., objects not originating from this pool) and duplicate returns of the same object.
- Support optional object resetting before reuse via a configurable functor.
- Implement
- RAII Handle:
- Enforce Resource Acquisition Is Initialization (RAII) using a scoped handle. This handle must automatically return the object to the pool when it is destroyed.
- The handle must be movable but not copyable.
- Safe Shutdown:
- The pool's destructor must safely stop any ongoing operations, wake up all waiting threads, and release all acquired resources without introducing deadlocks.
- Concurrency Primitives:
- Avoid busy-waiting. Correctly utilize
std::mutex,std::condition_variable, andstd::atomic.
- Avoid busy-waiting. Correctly utilize
- ABA Problem Prevention:
- Implement mechanisms to prevent ABA issues, such as using generation counters per item.
- Testing:
- Include minimal unit tests demonstrating correctness under multiple threads.
- Documentation:
- Document the time complexity of all core operations.
Deliverables
- A single, self-contained C++17 implementation of the object pool.
- Minimal unit tests.
- Brief notes on time complexity.