1. Problem It Solves
The stop-state types let one component request cancellation, observers query it, and callbacks react promptly without polling. It makes an important assumption visible and checkable.
2. Prerequisites
stop_source, stop_token, and callback lifetime.
You should be able to compile a short program and read its output.
3. Core Idea
A stop source controls one shared alarm, tokens listen to its state, and callbacks are bells wired to ring when the alarm is triggered. Read std::stop_callback as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
std::stop_callback callback{token, [] { /* wake or mark */ }};5. How It Works
The program introduces the smallest relevant form of
std::stop_callback.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
Cancellation remains cooperative; callback code must be fast, thread-safe, and prepared to run during registration if stop was already requested.
Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when several operations share a cancellation signal or blocked work needs a wake-up hook.
Avoid it when cleanup cannot safely occur in callback context or hard termination is being assumed.
8. Simple Example
A stop source owns the state, one callback flips an atomic flag, and requesting stop produces a deterministic result. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/39_cooperative_cancellation_stop_callback/main.cpp
// Day 39: Cooperative Cancellation and stop_callback
#include <atomic>
#include <iostream>
#include <stop_token>
int main() {
std::stop_source source;
std::stop_token token = source.get_token();
std::atomic<bool> callback_ran{false};
std::stop_callback callback{token, [&] {
callback_ran.store(true);
}};
bool first_request = source.request_stop();
std::cout << std::boolalpha;
std::cout << "first request = " << first_request << '\n';
std::cout << "stop requested = " << token.stop_requested() << '\n';
std::cout << "callback ran = " << callback_ran.load() << '\n';
}
9. Key Takeaways
std::stop_callbackexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
std::stop_callbackin the minimal example?Medium — What value does
request_stop()return on the first successful request, and what does the token report afterward?Hard — Why must callback lifetime and captured references remain valid even when registration happens after a stop request?