Namespaces
Variants

std::execution::on

From cppreference.com
 
 
Execution control library
Concepts
Utility components
Execution contexts
Execution domains
Forward progress guarantee
Environments
    
    
    
Queries
Completion signatures
Pipeable sender adaptors
Coroutine utility
 
Defined in header <execution>
struct on_t { /* unspecified */ };
(1) (since C++26)
inline constexpr on_t on{};
(2) (since C++26)
(customization point object)
Call signatures
execution::sender auto on( execution::scheduler auto sch,
                           execution::sender auto sndr );
(3) (since C++26)
execution::sender auto on( execution::sender auto sndr,
                           execution::scheduler auto sch,
                           execution::sender_adaptor_closure closure );
(4) (since C++26)

Runs part of a sender chain on a given scheduler and then returns to where it was. Both forms are “there and back again”: unlike starts_on and continues_on, which each move execution one way, on remembers the execution resource it came from and transfers execution back to it.

3) Starts sndr on an execution agent of sch's execution resource. When sndr completes, transfers execution back to the execution resource on which the on sender was started, and forwards the result of sndr there.
4) Lets sndr run where it runs. When sndr completes, transfers execution to sch's execution resource and runs the adaptor closure there with the result of sndr (the closure is applied to a sender that completes immediately with that result). When the closure's sender completes, transfers execution back to the execution resource where sndr completed, and forwards the closure's result there.

on is a pipeable sender adaptor object: form (4) can be written sndr | on(sch, closure).

Parameters

sch - the scheduler whose execution resource runs the work
sndr - the sender to run on sch (form (3)), or whose result is processed on sch (form (4))
closure - a sender_adaptor_closure (for example then(f)) to apply on sch to the result of sndr

Return value

A sender that behaves as described above. The result of the sender (values, error, or stopped signal) is the result of sndr in form (3) and the result of closure in form (4).

If any of the scheduling steps fails, an error completion is delivered on an unspecified execution agent.

Form (3) is ill-formed if sch is not an execution::scheduler, or if sndr is not a execution::sender (or is both a sender and a sender adaptor closure). Form (4) is ill-formed if sch is not a scheduler, sndr is not a sender, or closure is not a pipeable sender adaptor closure object.

Customization point objects

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

Original scheduler

The scheduler on returns to depends on the form:

  • Form (3) returns to the scheduler the on sender is started on: the result of get_start_scheduler for the environment of the receiver it is connected to. If that environment does not provide one, the sender cannot be used with that receiver.
  • Form (4) returns to the scheduler on which sndr completes, the value completion scheduler of sndr. If sndr does not report one, the sender cannot be used with that receiver. Senders that complete somewhere unknown, such as the result of starts_on, do not report one.

Notes

  • Form (3) is starts_on(sch, sndr) followed by a hop back to the scheduler the operation started on. Form (4) is continues_on(sndr, sch), then closure applied to the result, then a hop back to where sndr completed.
  • on is the algorithm to reach for when work should be “lent” to another execution resource but the surrounding code must keep running where it was: offloading a computation to a thread pool in the middle of a chain, or running a nested piece of work on a different scheduler.
  • An earlier version of the proposal used the name on for what is now starts_on. Programmers commonly assumed it was there-and-back-again and were surprised that the continuation ran on the scheduler the work had moved to, so starts_on and continues_on got names that say they are one-way, and on now means what people expected.

Example

Form (3) against starts_on: both run the work on the worker thread, but only on brings the continuation back to the main thread.
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 <iostream>
#include <string>
#include <thread>

namespace ex = std::execution;

int main()
{
    ex::run_loop worker_loop;
    std::jthread worker([&] { worker_loop.run(); });

    auto where = [&]
    {
        return std::this_thread::get_id() == worker.get_id() ? "worker thread"
                                                              : "main thread";
    };

    auto work = ex::just() | ex::then(where);
    auto describe = [&](auto w) { return std::string(w) + ", continues on the " + where(); };

    auto one_way = ex::starts_on(worker_loop.get_scheduler(), work)
                 | ex::then(describe);
    auto round_trip = ex::on(worker_loop.get_scheduler(), work)
                    | ex::then(describe);

    auto [a] = std::this_thread::sync_wait(std::move(one_way)).value();
    auto [b] = std::this_thread::sync_wait(std::move(round_trip)).value();

    std::cout << "starts_on: runs on the " << a << '\n'
              << "on:        runs on the " << b << '\n';

    worker_loop.finish();
}

Output:

starts_on: runs on the worker thread, continues on the worker thread
on:        runs on the worker thread, continues on the main thread

Form (4): only the middle step runs on the worker thread. The steps before and after it run where the chain started.
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 <iostream>
#include <thread>

namespace ex = std::execution;

int main()
{
    ex::run_loop worker_loop;
    std::jthread worker([&] { worker_loop.run(); });

    auto where = [&]
    {
        return std::this_thread::get_id() == worker.get_id() ? "worker thread"
                                                              : "main thread";
    };

    auto snd = ex::just(21)
             | ex::then([&](int n)
               {
                   std::cout << "produce on the " << where() << '\n';
                   return n;
               })
             | ex::on(worker_loop.get_scheduler(),
                        ex::then([&](int n)
                        {
                            std::cout << "crunch on the " << where() << '\n';
                            return n * 2;
                        }))
             | ex::then([&](int n)
               {
                   std::cout << "consume on the " << where() << '\n';
                   return n;
               });

    auto [result] = std::this_thread::sync_wait(std::move(snd)).value();
    std::cout << "result " << result << '\n';

    worker_loop.finish();
}

Output:

produce on the main thread
crunch on the worker thread
consume on the main thread
result 42

See also

adapts a provided sender into one that will start an execution on the provided scheduler's execution resource
(customization point object)[edit]
adapts a provided sender into one that completes on the provided scheduler's execution resource
(customization point object)[edit]
adapts a sender into one that completes on the scheduler the operation was started on
(customization point object)[edit]
asks a queryable object for the scheduler an operation will be or was started on
(customization point object)[edit]