Unspecified order of std::jthread stop callbacks on request_stop()
27K reputation · 19 Apr 2026, 13:42 UTC
Unspecified order of std::jthread stop callbacks on request_stop()
When a std::jthread registers multiple stop callbacks via std::stop_token::register_callback, the C++20 standard does not specify the order in which those callbacks are invoked when request_stop() is called.
Additionally, the standard does not mandate whether the callbacks execute in the thread that calls request_stop() or in separate, implementation‑defined threads. Many compilers run callbacks in the requesting thread, but this is not guaranteed.
Because the order and execution context are implementation‑dependent, developers cannot rely on a deterministic cleanup sequence when multiple callbacks are registered; this may cause race conditions or deadlocks if callbacks perform blocking operations.
- In what order are multiple stop callbacks invoked when
request_stop()is called? - Does
request_stop()block until all registered callbacks have completed? - Are stop callbacks executed in the thread that initiates cancellation, or are they dispatched to separate threads?