About this question

Cooperative Thread Cancellation with jthread

Easy · concurrency · Quant Developer interview question · c++20, concurrency, jthread, stop-token, cooperative-cancellation, threading, low-latency

C++20 std::jthread and std::stop_token provide a standardised mechanism for cooperative cancellation — an essential pattern in HFT infrastructure where market-data subscriber threads must respond to shutdown signals without corrupting shared state. Unlike forcible termination, cooperative cancellation lets a thread reach a safe checkpoint before exiting, enabling proper cleanup of open socket connections, order-acknowledgement buffers, and in-flight sequence-number state. Gateway processes at el