Implementing a thread-safe shared_ptr from scratch
This is a medium-difficulty C++ implementation problem common at quant trading firms, where deep knowledge of memory management and concurrency is essential. It tests whether you can build a reference-counted smart pointer that handles ownership transfer, lifetime management, and thread safety all at once.
The core challenge is designing a control block that tracks how many pointers own the same object, ensuring that the object is destroyed only when the last owner releases it. You must also guard that count against concurrent access using synchronisation primitives. Beyond the mechanics, the problem rewards careful handling of move vs. copy semantics, proper cleanup in destructors and reset operations, and correct operator overloading for dereferencing and member access.
- Reference counting and lifetime semantics
- Move and copy constructors and assignment operators
- Mutex-based synchronisation for shared state
- Memory safety and leak prevention
- Operator overloading for smart-pointer idioms