std::execution::operation_state, std::execution::operation_state_tag
| Defined in header <execution>
|
||
template< class O >
concept operation_state =
std::derived_from<typename O::operation_state_concept,
operation_state_tag> &&
requires (O& o)
{
execution::start(o);
};
|
(1) | (since C++26) |
| Helper tag type |
||
struct operation_state_tag {};
|
(2) | (since C++26) |
operation_state is satisfied by types of operation states: objects that hold an asynchronous operation that is ready to run. Connecting a sender to a receiver with connect creates an operation state, which contains everything the operation needs: the receiver, the data of the work, and the operation states of the child operations that the work starts. Nothing runs until execution::start(op) is called on the operation state op; the operation then eventually completes by calling one of the completion functions on its receiver.
- A type
Osatisfiesoperation_stateif all of the following hold:
- It has a member type
operation_state_conceptthat isoperation_state_tagor a class derived from it. execution::start(op)is valid for an lvalueopof typeO. It calls the member functionop.start(), which must benoexceptand returnvoid.
operation_state_tag, or a class derived from it, as its member type operation_state_concept.Lifetime
An operation state is started at most once. Once it has been started, it must stay alive until the operation completes: the behavior is undefined if it is destroyed after start begins and before a completion function is called on its receiver. Once the operation calls a completion function, the operation state can be destroyed, even before that function returns, for example by the code that the receiver resumes. The operation must therefore not access the operation state after it has called the completion function.
An operation state that has not been started can be destroyed at any time. For most senders, the work then never runs, because it only begins when the operation state is started.
The concept places no requirements on operations other than start and destruction. Copying or moving an operation state created by connecting a sender of the standard library is ill-formed, and copying or moving other operation states can lead to undefined behavior.
Notes
- Most programs do not use operation states directly: this_thread::sync_wait, spawn, spawn_future, and
co_awaitin a task connect a sender and start the operation state. Operation states are written in senders and sender algorithms. - To write an operation state, define a type with:
using operation_state_concept = std::execution::operation_state_tag;;- a
noexceptmember functionstart()that returnsvoid. It is called on an lvalue, so it can be declared&. It cannot throw, so an error that happens while starting the work is reported by calling set_error on the receiver.
- Since an operation state does not move while the operation runs, the operation can hand out pointers to it: a receiver that it connects to a child sender can point back to it, and it can register a stop callback, or link itself into a queue of work, as run_loop does, without allocating memory. The operation state of a sender adaptor contains the operation states of its children as members, so the whole chain of senders becomes one object.
sync_waitkeeps it on the stack, and running the chain needs no allocation. - An operation state that cannot be moved can still be returned from a function as a prvalue, and initialized from one, because of guaranteed copy elision:
auto op = execution::connect(sndr, rcvr);and the initialization of a member from the result ofconnectdo not move it. To create one later, or to create a new one in the place of an old one, std::optional can be used:emplaceconstructs the object from an argument that converts to the result ofconnect, through a conversion function that callsconnect. execution::connect_result_t<Sndr, Rcvr>is the type of the operation state created by connecting a sender of typeSndrto a receiver of typeRcvr.operation_state_tagwas calledoperation_state_tin earlier drafts.
Example
retry is a sender adaptor that runs its sender again when it completes with an error. Its operation state holds the operation state of the current attempt in a std::optional, and replaces it with a new one after each failure. The receiver of each attempt points back to the operation state of retry.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).
#include <exception>
#include <execution>
#include <optional>
#include <print>
#include <stdexcept>
#include <thread>
#include <utility>
namespace ex = std::execution;
// Runs the work again until it completes without an error
template<class Sndr>
struct retry
{
using sender_concept = ex::sender_tag;
Sndr sndr;
template<class Self, class... Env>
static consteval auto get_completion_signatures()
{
return ex::completion_signatures<ex::set_value_t(int),
ex::set_error_t(std::exception_ptr)>();
}
template<class Rcvr>
struct operation
{
// The receiver of each attempt points back to this operation state,
// which does not move while the operation runs
struct attempt_receiver
{
using receiver_concept = ex::receiver_tag;
operation* op;
void set_value(int v) && noexcept
{
ex::set_value(std::move(op->rcvr), v);
}
// The operation state of the failed attempt can be destroyed
// during this call
void set_error(auto) && noexcept { op->next_attempt(); }
auto get_env() const noexcept { return ex::get_env(op->rcvr); }
};
using operation_state_concept = ex::operation_state_tag;
Sndr sndr;
Rcvr rcvr;
// The operation state of the current attempt
using attempt_op = ex::connect_result_t<Sndr&, attempt_receiver>;
std::optional<attempt_op> attempt;
void start() & noexcept { next_attempt(); }
void next_attempt() noexcept
{
// An operation state cannot be moved, so emplace constructs it
// from the result of a conversion function that calls connect
struct connector
{
operation* op;
operator attempt_op() const
{
return ex::connect(op->sndr, attempt_receiver{op});
}
};
try
{
// Destroys the operation state of the failed attempt, if any,
// and constructs a new one in its place
attempt.emplace(connector{this});
ex::start(*attempt);
}
catch (...)
{
ex::set_error(std::move(rcvr), std::current_exception());
}
}
};
template<class Rcvr>
operation<Rcvr> connect(Rcvr rcvr) const
{
return {sndr, std::move(rcvr)};
}
};
int main()
{
int attempts = 0;
auto flaky = ex::just() | ex::then([&attempts]
{
if (++attempts < 3)
throw std::runtime_error("busy");
return attempts;
});
auto [n] = std::this_thread::sync_wait(retry{flaky}).value();
std::println("Succeeded on attempt {}", n);
}
Output:
Succeeded on attempt 3
See also
(C++26) |
connects a sender with a receiver (customization point object) |
(C++26) |
starts the asynchronous operation associated with an operation_state object (customization point object) |
(C++26) |
specifies that a type is a sender, a description of asynchronous work (concept) |
(C++26) |
specifies that a type is a receiver (concept) |
| specifies that a receiver can be recreated from the address of the operation state it was connected to (concept) |