Namespaces
Variants

std::execution::parallel_scheduler_replacement::receiver_proxy

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
struct receiver_proxy
{
protected:
    virtual bool /*query-env*/( /* unspecified */ ) noexcept = 0; // exposition only

public:
    virtual void set_value() noexcept = 0;
    virtual void set_error( std::exception_ptr ) noexcept = 0;
    virtual void set_stopped() noexcept = 0;

    template< class P, /*class-type*/ Query >
    std::optional<P> try_query( Query q ) const noexcept;
};
(since C++26)

receiver_proxy stands for the receiver of an operation that parallel_scheduler hands over to a parallel_scheduler_backend. When the sender returned by schedule for a parallel_scheduler is connected to a receiver and started, the scheduler calls schedule(r, s) on its backend, where r is a receiver_proxy for that receiver. The backend completes the operation by calling one of the member functions set_value, set_error and set_stopped on r, and can ask for properties of the receiver, such as its stop token, with try_query.

For the bulk algorithms, the backend receives a bulk_item_receiver_proxy, which is derived from receiver_proxy and also runs the iterations of the algorithm.

The objects are created by parallel_scheduler, not by the backend. A program cannot create its own, since it cannot override the exposition-only function /*query-env*/, whose signature is unspecified.

Member functions

parallel_scheduler_replacement::receiver_proxy::set_value

virtual void set_value() noexcept = 0;
(since C++26)

Completes the operation successfully: calls execution::set_value on the receiver.

A backend must call it on an execution agent of its execution resource.

parallel_scheduler_replacement::receiver_proxy::set_error

virtual void set_error( std::exception_ptr e ) noexcept = 0;
(since C++26)

Completes the operation with an error: calls execution::set_error on the receiver with std::move(e). A backend calls it when it cannot run the work, for example because it cannot allocate memory or start a thread. It can be called on any thread.

parallel_scheduler_replacement::receiver_proxy::set_stopped

virtual void set_stopped() noexcept = 0;
(since C++26)

Completes the operation without running the work: calls execution::set_stopped on the receiver. A backend calls it when stop was requested on the stop token of the receiver. It can be called on any thread.

parallel_scheduler_replacement::receiver_proxy::try_query

template< class P, /*class-type*/ Query >
std::optional<P> try_query( Query q ) const noexcept;
(since C++26)

Asks the environment of the receiver for a property: q is the query object, such as execution::get_stop_token, and P is the type of the result that the backend wants. Let env be the environment of the receiver.

  • If the implementation does not support the query Query, or does not support results of type P for it, or q(env) is not a valid expression, returns std::nullopt.
  • Otherwise, if q(env) has type P (possibly cv-qualified), returns q(env).
  • Otherwise, returns an implementation-defined value, which can be std::nullopt.

The set of supported queries, and the set of supported result types for each, are implementation-defined. They always include execution::get_stop_token_t with the result type std::inplace_stop_token, so a backend can always get the stop token with r.try_query<std::inplace_stop_token>(execution::get_stop_token).

If the stop token of the receiver has a different type, such as std::stop_token, implementations are encouraged to return an std::inplace_stop_token on which stop is requested when it is requested on the stop token of the receiver, and to return equivalent tokens from every call until the operation completes.

This overload participates in overload resolution only if Query is a class type. If P is not a non-array object type that is not cv-qualified, the program is ill-formed.

Notes

  • The completion functions are noexcept, and exactly one of them must be called for each operation. As soon as one of them is called, the operation can end, and the receiver_proxy object can be destroyed. A backend must not use the object after calling it. The class has no virtual destructor, because the backend never destroys the object.
  • The backend and the rest of the program can be compiled separately, so the backend cannot know the type of the receiver or of its environment. That is why try_query takes the result type as a template argument, and supports only a fixed set of queries and result types, chosen by the implementation.
  • Checking the stop token is optional. It is useful for a backend that keeps work in a queue: stop can be requested after the work was handed over to the backend, but before it runs.
  • The namespace parallel_scheduler_replacement was called system_context_replaceability until it was renamed by P4154R0, before C++26 was published. P3804R2 made try_query const, removed the virtual destructor, and allowed try_query to adapt stop tokens of other types. Older implementations and articles may show the earlier interface.

Example

A backend keeps work in a queue until run_pending is called. Stop is requested for the second operation after it was handed over to the backend, so the backend completes it with set_stopped instead of running it.
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 <cstddef>
#include <exception>
#include <execution>
#include <memory>
#include <print>
#include <span>
#include <stop_token>
#include <utility>
#include <vector>

namespace ex = std::execution;
namespace psr = std::execution::parallel_scheduler_replacement;
using bulk_proxy = psr::bulk_item_receiver_proxy;
using storage = std::span<std::byte>;

// A backend that keeps work in a queue until run_pending() is called. Work
// whose receiver was asked to stop in the meantime does not run
struct queue_backend : psr::parallel_scheduler_backend
{
    std::vector<psr::receiver_proxy*> pending;

    void schedule(psr::receiver_proxy& r, storage) noexcept override
    {
        try { pending.push_back(&r); }
        catch (...) { r.set_error(std::current_exception()); }
    }

    void run_pending()
    {
        for (psr::receiver_proxy* r : std::exchange(pending, {}))
        {
            auto stop =
                r->try_query<std::inplace_stop_token>(ex::get_stop_token);
            if (stop && stop->stop_requested())
                r->set_stopped();
            else
                r->set_value();
        }
    }

    // Bulk work is not queued: it runs at once
    void schedule_bulk_chunked(std::size_t n, bulk_proxy& r,
                               storage s) noexcept override
    {
        schedule_bulk_unchunked(n, r, s);
    }

    void schedule_bulk_unchunked(std::size_t n, bulk_proxy& r,
                                 storage) noexcept override
    {
        for (std::size_t i = 0; i != n; ++i)
            r.execute(i, i + 1);
        r.set_value();
    }
};

auto backend = std::make_shared<queue_backend>();

std::shared_ptr<psr::parallel_scheduler_backend>
psr::query_parallel_scheduler_backend() { return backend; }

// A receiver that prints how the work completed
struct print_receiver
{
    using receiver_concept = ex::receiver_t;
    const char* name;
    std::inplace_stop_token stop;

    void set_value() noexcept { std::println("{}: ran", name); }
    void set_error(std::exception_ptr) noexcept { std::println("error"); }
    void set_stopped() noexcept { std::println("{}: stopped", name); }
    auto get_env() const noexcept { return ex::prop(ex::get_stop_token, stop); }
};

int main()
{
    std::inplace_stop_source first, second;
    auto sch = ex::get_parallel_scheduler();
    auto op1 = ex::connect(ex::schedule(sch),
                           print_receiver{"first", first.get_token()});
    auto op2 = ex::connect(ex::schedule(sch),
                           print_receiver{"second", second.get_token()});
    ex::start(op1);
    ex::start(op2);

    second.request_stop(); // after the work was handed over to the backend
    std::println("{} operations pending", backend->pending.size());
    backend->run_pending();
}

Output:

2 operations pending
first: ran
second: stopped

See also

serves as the abstraction bridge between the user-facing parallel_scheduler and the backend execution resource (such as a OS-provided system thread pool)
(class) [edit]
used for bulk_chunked and bulk_unchunked customizations that will also receive notifications from implementations of parallel_scheduler_backend corresponding to different iterations
(class) [edit]
returns the backend object that every parallel_scheduler uses; a program can replace it to provide its own backend
(function) [edit]
represents a handle of a shared global execution resource (such as a OS-provided system thread pool) optimized for parallel execution
(class) [edit]
asks a queryable object for the stop token of an asynchronous operation, or obtains the type of that token
(customization point object)[edit]
a stop token that references stop state of its associated std::inplace_stop_source object
(class) [edit]