std::execution::read_env
| Defined in header <execution>
|
||
inline constexpr /* unspecified */ read_env{};
|
(1) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto read_env( auto query );
|
(2) | (since C++26) |
A sender factory: read_env(q) returns a sender that, when started, looks up the query q in the execution environment of the receiver it is connected to, and completes with the result as its value.
Every receiver has an associated execution environment, an unstructured collection of values that can be looked up by query: which scheduler to use, which allocator, which stop token, and any queries a library defines. It is how a parent operation passes implicit parameters to the operations nested inside it. The environment is known only when the sender is connected to a receiver, not when the sender is built, so a sender cannot simply call get_scheduler(env) itself: there is no environment to ask yet. read_env is the sender that does the asking at the right time, and turns the answer into an ordinary value that the next step of the pipeline can use. write_env is its counterpart: it changes what the environment contains for the operations nested in a sender.
Parameters
| query | - | a query object, such as get_scheduler, get_stop_token or get_allocator; it is called with the environment, and the result is what the sender completes with |
Return value
A sender that completes in its start operation (synchronously, on the thread that starts it) with a single value, query(env), where env is the environment of the receiver it is connected to. It cannot complete with any other value.
If calling the query throws, the sender completes with the exception as an error (set_error with a std::exception_ptr).
The sender can only be connected to a receiver whose environment answers the query, and the result must not be void. Otherwise, the program is ill-formed, and this is diagnosed at compile time when the sender is connected.
Customization point objects
The name execution::read_env denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details.
Details
- Nothing is read when the sender is built. The query is evaluated when the sender is started, against the environment of the receiver at that point, so the same sender can give different answers when connected to different receivers.
- The completion signature of the sender depends on the receiver: the type of the value is the type that the query returns for that environment. Senders whose result type is not known until they are connected to a receiver are called dependent senders (dependent_sender);
read_envis the standard one. - The value is the answer of the query as it is, copied or moved into the completion; for a query that returns a reference, the sender completes with a copy of the object.
Notes
- The usual use is to read a value out of the environment and then do something with it using let_value: for example, to run more work on the scheduler that the surrounding operation provides, or to hand the allocator or the stop token to something that needs it explicitly (see the example).
- The environment of a receiver is not the environment of the sender before it is connected. Inside a pipeline, what
read_envsees is the environment of the whole operation, as modified by any write_env that encloses it. - Earlier versions of the proposal allowed calling a query with no arguments (for example
get_scheduler()) to get such a sender. That form was removed in favor of the single, explicit spellingread_env(get_scheduler).
Example
sync_wait provides a scheduler to the sender it runs. read_env(get_scheduler) reads it out, and let_value uses it to start nested work on that scheduler, here the thread that called sync_wait.
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 <print>
#include <thread>
namespace ex = std::execution;
int main()
{
const auto main_id = std::this_thread::get_id();
auto sndr = ex::read_env(ex::get_scheduler)
| ex::let_value([](auto sched)
{
// sched is the scheduler that sync_wait provides to the sender
return ex::starts_on(
sched, ex::just()
| ex::then([] { return std::this_thread::get_id(); })
);
});
auto [id] = std::this_thread::sync_wait(std::move(sndr)).value();
std::println("The nested work ran on the thread that called sync_wait: {}", id == main_id);
}
Output:
The nested work ran on the thread that called sync_wait: true
See also
(C++26) |
adapts a sender so that the operations inside it see extra or different values in their execution environment (customization point object) |
(C++26) |
asks a queryable object for its associated scheduler (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and continuing with the sender it returns (customization point object) |