std::execution::just_stopped
| Defined in header <execution>
|
||
struct just_stopped_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr just_stopped_t just_stopped{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto just_stopped();
|
(3) | (since C++26) |
Returns a sender that, when started, immediately completes with the stopped signal.
The stopped signal means that an operation ended without succeeding and without failing, usually because it was cancelled: its result is no longer needed. just_stopped puts that outcome into a sender chain. Its main use is inside the function passed to let_value, let_error or let_stopped, where returning just_stopped() ends the work quietly instead of producing a result or reporting an error: for example, when a request has timed out and its result would no longer be used. It is also useful for testing how sender-based code handles cancellation, and as a stand-in for an operation that was cancelled.
Parameters
(none)
Return value
A sender that, when connected to a receiver and started, completes immediately with set_stopped(), on the execution agent that started it.
The sender never completes with a value or with an error.
Customization point objects
The name execution::just_stopped denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::just_stopped_t. See CustomizationPointObject for details.
Details
- The sender completes inline, inside the call that starts it, on the same thread. It can be started any number of times.
- Algorithms that follow the sender in a chain see only the stopped signal: then, upon_error and the other handlers for values and errors are skipped, and the signal is passed on until something handles it, such as upon_stopped or let_stopped.
just_stoppedreports that work was stopped; it does not ask any other work to stop. Algorithms that run several senders may react to it, though: when one sender in a when_all completes with the stopped signal,when_allasks the others to stop, and then completes with the stopped signal itself, unless one of them failed first.
Notes
- Being stopped is not an error, and algorithms treat it differently. spawn, which only accepts work that cannot fail, accepts a sender that can be stopped, and sync_wait returns an empty std::optional for it instead of throwing.
- The sender cannot complete with a value, so it cannot be waited for on its own:
this_thread::sync_wait(just_stopped())is a compile-time error, because sync_wait needs exactly one way of completing with values. The same holds for a when_all that contains it. Handle the signal first, for example with upon_stopped, which turns it into a value. - In a task coroutine,
co_await ex::just_stopped(), like awaiting any sender that completes with the stopped signal, does not resume the coroutine. The coroutine is destroyed, which runs the destructors of its local variables but enters nocatchblock, and the task itself completes with the stopped signal. This is the way to stop a coroutine depending on a run-time condition. just(value)andjust_stopped()have different types, so a function cannot return one or the other depending on a run-time condition.- stopped_as_optional and stopped_as_error turn the stopped signal into an empty std::optional or into an error.
- just and just_error do the same for the value and error channels.
Example
A sender made with just_stopped skips then and is handled by upon_stopped. Then just_stopped is returned from let_error to give up quietly after an error, and finally it is passed to spawn, which accepts it because being stopped is not an error.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.
#include <execution>
#include <iostream>
#include <string>
#include <system_error>
#include <thread>
namespace ex = std::execution;
int main()
{
auto report = [] { return std::string("Handled: the work was stopped"); };
// A sender that is stopped as soon as it is started. It sends no value,
// so then is skipped; upon_stopped turns the stopped signal into a value.
auto stopped = ex::just_stopped()
| ex::then([] { std::cout << "Not printed\n"; })
| ex::upon_stopped(report);
auto [a] = std::this_thread::sync_wait(std::move(stopped)).value();
std::cout << a << '\n';
// Returned from let_error, just_stopped gives up quietly instead of
// reporting the error, for example when a timed-out result is not needed.
auto give_up = [](std::error_code) noexcept { return ex::just_stopped(); };
auto timed_out = ex::just_error(std::make_error_code(std::errc::timed_out))
| ex::let_error(give_up)
| ex::upon_stopped(report);
auto [b] = std::this_thread::sync_wait(std::move(timed_out)).value();
std::cout << b << '\n';
// Being stopped is not an error: spawn, which only accepts work that
// cannot fail, accepts a sender that can be stopped.
ex::counting_scope scope;
ex::spawn(ex::just_stopped(), scope.get_token());
std::this_thread::sync_wait(scope.join());
std::cout << "Spawned work was stopped, and the scope was joined\n";
}
Output:
Handled: the work was stopped
Handled: the work was stopped
Spawned work was stopped, and the scope was joined
See also
(C++26) |
returns a sender that immediately sends copies of the given values (customization point object) |
(C++26) |
returns a sender that immediately sends a copy of the given error (customization point object) |
(C++26) |
chains a function onto a sender, calling it if the sender is cancelled and sending its result on as a value (customization point object) |
(C++26) |
chains a function onto a sender, calling it if the sender is cancelled and continuing with the sender it returns (customization point object) |
returns a sender that maps the value channel to std::optional<std::decay_t<T>> and the stopped channel to std::nullopt(customization point object) |