std::execution::write_env
| Defined in header <execution>
|
||
inline constexpr /* unspecified */ write_env{};
|
(1) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto write_env( execution::sender auto sndr,
/*queryable-object*/ auto env );
|
(2) | (since C++26) |
Adapts a sender so that the operations inside it see extra or different values in their execution environment: when the returned sender is connected to a receiver, sndr is connected to a receiver whose environment is env joined with the environment of the original receiver. Queries are answered by env first, and by the original environment if env has no answer.
Every receiver has an associated execution environment, an unstructured collection of values that can be looked up by query: which scheduler, 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, without every algorithm in between having to know about them. write_env is the explicit way to change what the children see, for example to give them an allocator, a different stop token, or a value of your own. read_env is its counterpart: it reads a value out of the environment.
Parameters
| sndr | - | the sender whose operations see the modified environment |
| env | - | a queryable object with the values to inject; the usual way to make one is prop, which pairs one query with one value, or env, which combines several |
Return value
A sender that behaves like sndr, with sndr (and everything nested in it) connected to a receiver whose environment answers a query q with env.query(q) if that is valid, and otherwise with the environment of the receiver that the returned sender is connected to. All completions of sndr are forwarded unchanged.
write_env(sndr, env) is ill-formed if sndr does not satisfy execution::sender or env is not a queryable object.
Customization point objects
The name execution::write_env denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details. Despite being a customization point object, write_env is not meant to be customized.
Details
- Nothing runs when the sender is built. The environment is joined when the returned sender is connected to a receiver.
- The injected values are visible to
sndrand to everything started inside it, including senders returned by functions passed to let_value and its relatives. - The sender that comes after
write_envin a pipeline does not see the injected values: they are part of the environment ofsndr, not of the environment the returned sender passes on. - An inner
write_envoverrides an outer one for the query it provides; queries it does not provide fall through to the outer environment. - Queries that
envdoes not answer are forwarded: writing a value for one query does not hide the others.
Notes
- The best-known use built on it is unstoppable, which is
write_envwith a stop token that never reports a stop request, which protects work that must run to completion, for example to restore an invariant. Several other algorithms in the library are specified in terms ofwrite_envas well. - A typical use in application code is to parameterize a sender made by a library with something the library asks for through the environment, such as an allocator, without changing the library: the library's sender reads the allocator with read_env, and the caller supplies it with
write_env(see the example). - Only forwarding queries are passed on from the environment of an operation to the operations it starts: a query is forwarded when
forwarding_query(q)istrue. A query that is not forwarding is not seen by operations nested insidesndr, only by those that are directly given the environment. The standard queries meant to apply to a whole subtree, such as the stop token and the allocator, are forwarding queries.
Example
A function that returns a sender asks its environment for an allocator and uses it. The caller supplies the allocator with write_env without changing the function, and the counter shows that the allocator that was injected is the one used.
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 <cstddef>
#include <execution>
#include <memory_resource>
#include <print>
#include <thread>
#include <vector>
namespace ex = std::execution;
// A memory resource that counts the allocations made through it.
struct counting_resource : std::pmr::memory_resource
{
std::size_t allocations = 0;
void* do_allocate(std::size_t bytes, std::size_t align) override
{
++allocations;
return std::pmr::new_delete_resource()->allocate(bytes, align);
}
void do_deallocate(void* p, std::size_t bytes, std::size_t align) override
{
std::pmr::new_delete_resource()->deallocate(p, bytes, align);
}
bool do_is_equal(const memory_resource& other) const noexcept override
{
return this == &other;
}
};
// A sender that builds a vector using whatever allocator its environment provides.
ex::sender auto make_numbers()
{
return ex::read_env(ex::get_allocator)
| ex::then([](auto alloc)
{
std::pmr::polymorphic_allocator<int> int_alloc(alloc.resource());
std::vector<int, std::pmr::polymorphic_allocator<int>> numbers(int_alloc);
for (int i = 0; i < 100; ++i)
numbers.push_back(i);
return numbers.size();
});
}
int main()
{
counting_resource resource;
auto sndr = ex::write_env(
make_numbers(),
ex::prop{ex::get_allocator,
std::pmr::polymorphic_allocator<std::byte>(&resource)});
auto [count] = std::this_thread::sync_wait(std::move(sndr)).value();
std::println("built {} numbers, used the injected allocator: {}",
count, resource.allocations > 0);
}
Output:
built 100 numbers, used the injected allocator: true
See also
(C++26) |
creates a sender that queries its receiver's associated environment (customization point object) |
(C++26) |
shields the provided sender from external cancellation requests, ensuring that the critical task runs uninterrupted to completion (customization point object) |