Namespaces
Variants

std::execution::schedule_from

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 schedule_from_t { /* unspecified */ };
(1) (since C++26)
inline constexpr schedule_from_t schedule_from{};
(2) (since C++26)
(customization point object)
Call signature
execution::sender auto schedule_from( execution::sender auto sndr );
(3) (since C++26)

A customization hook for scheduler authors. schedule_from(sndr) behaves exactly like sndr: it completes with the same result, on the same execution agent. Its only purpose is to give a scheduler (more precisely, its execution domain) a place to customize how work leaves the scheduler's execution context.

Application code normally never calls schedule_from. It is used by the implementation of continues_on: continues_on(sndr, sch) is built from schedule_from(sndr).

Parameters

sndr - the sender to wrap

Return value

A sender that behaves like sndr: value, error and stopped completions are forwarded unchanged, and sndr's completion scheduler (if it has one) is the completion scheduler of the result.

schedule_from(sndr) is ill-formed if sndr does not satisfy execution::sender.

Customization point objects

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

Customization

A transition between two execution contexts that know nothing about each other has two sides: leaving the first context and entering the second. Both sides can be customized, by different algorithms:

  • continues_on is customized by the scheduler it continues on, and controls how work enters a context.
  • schedule_from is customized by the context the wrapped sender completes on, and controls how work leaves it.

Because schedule_from adds no behavior of its own, wrapping a sender in it never changes what the sender does unless a domain customizes it. A scheduler for an accelerator is the typical case: after running work on a GPU, the results must be brought back to the CPU before a continuation can run, and only the scheduler that owns the GPU knows how to do that efficiently. It customizes schedule_from to perform that step, and anything built on continues_on picks it up without the user doing anything.

Customizations are found through the usual transform_sender mechanism, using the completion domain of the wrapped sender.

Notes

  • Earlier drafts of the proposal gave schedule_from two arguments, schedule_from(sch, sndr), and used it as the implementation of continues_on, with special handling in the customization machinery to dispatch it on the right domain. The one-argument form replaced that: continues_on now does the transfer, and schedule_from only exists to be customized.
  • Using schedule_from in your own sender chains is harmless but pointless: to move to another scheduler, use continues_on.

Example

schedule_from does not move work or change results: the continuation runs on the thread where the input completed, exactly as without it.
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 input = ex::schedule(worker_loop.get_scheduler()); // completes on the worker thread

    auto plain   = input                    | ex::then(where);
    auto wrapped = ex::schedule_from(input) | ex::then(where);

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

    std::cout << "input:                continues on the " << a << '\n'
              << "schedule_from(input): continues on the " << b << '\n';

    worker_loop.finish();
}

Output:

input:                continues on the worker thread
schedule_from(input): continues on the worker thread

See also

adapts a provided sender into one that completes on the provided scheduler's execution resource
(customization point object)[edit]
transforms into a new sender under a given execution domain tag
(function template) [edit]