Implementing std::jthread with stop-token support in C++
This is a hard coding problem that tests your ability to design and implement a modern C++ synchronization primitive from first principles. It combines thread lifecycle management, callable-type introspection, and lock-free state coordination—all of which appear in systems code at trading firms and infrastructure teams.
The problem requires you to build a joinable thread wrapper that automatically joins (and can cancel) on scope exit, avoiding the silent termination that can occur with raw std::thread. Key challenges include detecting whether a callable accepts a stop token at compile time, managing the transition from active to canceled state without locks, and ensuring thread safety across destruction, detachment, and external cancellation requests.
- SFINAE or
std::invoke_result for callable type checking
- Atomic flags and memory ordering for lock-free synchronization
- Thread lifecycle: construction, detach, join, and scope-based cleanup
- Implicit conversions and move semantics restrictions