Namespaces
Variants

std::execution::get_domain

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
struct get_domain_t { /* unspecified */ };
(1) (since C++26)
inline constexpr get_domain_t get_domain{};
(2) (since C++26)
(customization point object)
Call signature
template< class Env >
/*domain*/ get_domain( const Env& env ) noexcept;
(3) (since C++26)

A query that asks an environment, usually the environment of a receiver, for the execution domain in which an operation connected to that receiver starts.

A domain is a class whose member function transform_sender can replace a sender with another sender that does the same work in a way suited to a particular execution resource, for example one that runs the function given to bulk on a GPU. When a sender is connected to a receiver rcvr, connect passes it to transform_sender, which calls the transform_sender of the domain in which the operation completes, see get_completion_domain, and then of the domain in which it starts, get_domain(execution::get_env(rcvr)). The transform_sender of default_domain keeps the implementations of the standard library.

Most programs do not use domains directly. A domain is provided by the author of a scheduler for an execution resource that needs its own implementations of the standard algorithms, for example of bulk and the related algorithms. In the standard library, parallel_scheduler and task_scheduler have domains of their own, which pass the bulk algorithms to the thread pool or to the scheduler they use.

3) Returns a default-constructed object of the first of these types:
  • the decayed type of std::as_const(env).query(get_domain), if that expression is valid;
  • otherwise, the domain of the scheduler execution::get_scheduler(env), that is, the domain in which the work it schedules completes, if the environment has a scheduler;
  • otherwise, default_domain.
The call is therefore valid for any environment. The program is ill-formed if the query can throw an exception.

Parameters

env - the environment (or other queryable object) to query

Return value

An object of the domain type in which operations connected to a receiver with the environment env start.

Customization point objects

The name execution::get_domain 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::get_domain_t. See CustomizationPointObject for details.

Notes

  • Only the type of a domain matters: get_domain returns a default-constructed object of the type that the environment answers with, not the object itself. A domain type must therefore be default-constructible, and a domain cannot carry state: its member functions transform_sender and apply_sender are usually static.
  • The query is forwarded through the environments of the receivers of sender adaptors, so the senders nested in a sender started in a domain are connected in the same domain, unless an adaptor on the way changes it. let_value and the related algorithms connect the sender returned by their function in the domain in which their child sender completes.
  • An environment answers the query with a member function query(execution::get_domain_t) that returns an object of the domain type, or a program can add the answer with execution::write_env(sndr, execution::prop(execution::get_domain, dom)). A scheduler reports its own domain by answering execution::get_completion_domain<execution::set_value_t>, since the work it schedules completes on its execution resource.
  • get_domain tells where an operation starts, and get_completion_domain tells where a sender completes. A sender that completes where it is started, such as the one returned by just, completes in the domain of the environment it is started in.
  • In earlier drafts of C++26, get_domain was also asked of the attributes of a sender, with get_domain(execution::get_env(sndr)), and sender algorithms used the answer to replace a sender as soon as they created it. A sender often cannot know where it completes until it knows where it starts, so these answers were often wrong. P3826R5 replaced this use with get_completion_domain, which is told the environment of the receiver, and made get_domain fall back to the domain of the scheduler of the environment and to default_domain, before C++26 was published. Older implementations and articles show the earlier interface.

Example

tracing_domain replaces no algorithm: its transform_sender prints a line and returns the sender unchanged, so it shows when senders are connected in that domain. write_env makes the sender start in tracing_domain.
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 <concepts>
#include <execution>
#include <print>
#include <tuple>
#include <utility>

namespace ex = std::execution;

// A domain that reports the senders that are connected in it
struct tracing_domain
{
    template<ex::sender Sndr, class Env>
    static Sndr transform_sender(ex::start_t, Sndr&& sndr, const Env&)
    {
        std::println("Connecting a sender in tracing_domain");
        return std::forward<Sndr>(sndr);
    }
};

int main()
{
    // An environment that does not name a domain or a scheduler
    static_assert(std::same_as<decltype(ex::get_domain(ex::env<>{})),
                               ex::default_domain>);

    // The sender reads the domain in which it is started. With write_env, the
    // then sender and the read_env sender inside it are both connected, and
    // started, in tracing_domain
    auto sndr = ex::read_env(ex::get_domain) | ex::then([](auto dom)
    {
        return std::same_as<decltype(dom), tracing_domain>;
    });
    auto env = ex::prop(ex::get_domain, tracing_domain{});
    auto result = std::this_thread::sync_wait(ex::write_env(sndr, env));
    std::println("Started in tracing_domain: {}", std::get<0>(result.value()));
}

Output:

Connecting a sender in tracing_domain
Connecting a sender in tracing_domain
Started in tracing_domain: true

See also

asks the completion domain associated with a completion tag from a sender's attributes
(customization point object)[edit]
execution domain of the standard library's own implementations of the sender algorithms
(class) [edit]
transforms into a new sender under a given execution domain tag
(function template) [edit]
asks a queryable object for its associated scheduler
(customization point object)[edit]
adapts a sender so that the operations inside it see extra or different values in their execution environment
(customization point object)[edit]