Namespaces
Variants

std::execution::continues_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 continues_on_t { /* unspecified */ };
(1) (since C++26)
inline constexpr continues_on_t continues_on{};
(2) (since C++26)
(customization point object)
Call signature
execution::sender auto continues_on( execution::sender auto sndr,
                                     execution::scheduler auto sch );
(since C++26)

Adapts a sender so that it completes on the execution resource of a given scheduler. Whatever the adapted sender produces (values, an error, or a stopped signal) is handed over to sch, and the continuation of the returned sender runs there.

continues_on is how a sender chain moves from one execution resource to another. The adapted sender runs wherever it runs; only where the chain continues is changed. This is the counterpart of starts_on, which controls where a sender starts and does not care where it completes.

continues_on can be used in pipelines: sndr | continues_on(sch) is equivalent to continues_on(sndr, sch).

Parameters

sndr - the sender whose completion is moved to sch
sch - the scheduler to continue on

Return value

A sender that starts sndr on the current execution agent and, when sndr completes, schedules the completion onto sch and completes there with the same result (values, error, or stopped signal). The returned sender reports sch as its value completion scheduler, so algorithms that follow it in a chain (for example then) know where they run.

continues_on(sndr, sch) is ill-formed if sch does not satisfy execution::scheduler or sndr does not satisfy execution::sender.

Customization point objects

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

Details

  • The values sent by sndr are copied (or moved) into the operation state before the hop to sch, so they must be copyable in the std::decay_t sense. If that copy can throw, the returned sender can also complete with set_error(std::exception_ptr).
  • The operation for scheduling onto sch is created when the returned sender is connected, not when sndr completes.
  • The receiver's stop token is forwarded to the scheduling operation. If stop is requested and sch honors it, the hop to sch can complete with set_stopped(), and that completion is not guaranteed to happen on sch.
  • If scheduling onto sch fails, an error completion is delivered on an unspecified execution agent, because there is no agent of sch to deliver it on.
  • continues_on makes no promise to skip the hop when the completion is already on sch's execution resource, and the hop can fail or be stopped. affine is the algorithm to use when the destination is the scheduler the operation started on: it avoids unneeded hops where it can, and it requires a scheduler on which hopping cannot fail.

Notes

  • continues_on is specified in terms of a second adaptor, schedule_from: continues_on(sndr, sch) builds a sender around schedule_from(sndr). schedule_from is used by schedulers to control how work transitions off their execution contexts, and is not meant to be called directly.
  • continues_on always goes to the scheduler you pass. To always return to the scheduler an operation was started on, use affine; to run a sender on a given scheduler and then return to the original one, use on.
  • Work never changes execution resource unless something in the chain asks for it. This is deliberate: a sender that does not use continues_on (or an algorithm built on it) completes where it ran, so the code after it runs where its author expects.
  • This algorithm was called transfer in earlier drafts.

Example

Read on an I/O thread, then process on a worker thread. starts_on chooses where the read runs, and continues_on chooses where the chain continues.
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 io_loop, work_loop;
    std::jthread io_thread([&] { io_loop.run(); });
    std::jthread work_thread([&] { work_loop.run(); });

    auto where = [&]
    {
        const auto id = std::this_thread::get_id();
        return id == io_thread.get_id()   ? "io thread"
             : id == work_thread.get_id() ? "work thread"
                                          : "other thread";
    };

    auto read = ex::just(42)
              | ex::then([&](int n)
                {
                    std::cout << "read " << n << " on the " << where() << '\n';
                    return n;
                });

    auto snd = ex::starts_on(io_loop.get_scheduler(), read)
             | ex::continues_on(work_loop.get_scheduler())
             | ex::then([&](int n)
               {
                   std::cout << "processed " << n << " on the " << where() << '\n';
               });

    std::this_thread::sync_wait(std::move(snd));

    io_loop.finish();
    work_loop.finish();
}

Output:

read 42 on the io thread
processed 42 on the work thread

See also

adapts a sender into one that completes on the scheduler the operation was started on
(customization point object)[edit]
adapts a sender to run it (or a continuation of it) on the provided scheduler, then return to the scheduler it came from
(customization point object)[edit]