std::execution::starts_on
| Defined in header <execution>
|
||
struct starts_on_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr starts_on_t starts_on{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto starts_on( execution::scheduler auto sch,
execution::sender auto sndr );
|
(since C++26) | |
Adapts a sender so that it is started on an execution agent of the execution resource of a given scheduler.
starts_on chooses where a sender starts. It says nothing about where the sender completes: the work normally completes wherever it finishes, and the continuation of the returned sender runs there, not back on the original scheduler. This is the counterpart of continues_on, which chooses where a chain continues. To run a sender on a given scheduler and then return to the scheduler the operation was started on, use on.
Parameters
| sch | - | the scheduler on whose execution resource sndr is started
|
| sndr | - | the sender to start there |
Return value
A sender that, when started, first moves to an execution agent of sch's execution resource and then starts sndr there. The result of sndr (values, error, or stopped signal) is forwarded to the receiver of the returned sender unchanged, on whichever execution agent sndr completes on.
starts_on(sch, sndr) is ill-formed if sch does not satisfy execution::scheduler or sndr does not satisfy execution::sender.
Customization point objects
The name execution::starts_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::starts_on_t. See CustomizationPointObject for details.
Details
- While
sndrruns, the scheduler it was started on issch: a query for execution::get_start_scheduler in the environment ofsndr's receiver yieldssch. Algorithms insidesndrthat return to where they started, such as affine or on, therefore return tosch, not to the scheduler the outer operation was started on. - If moving to
schfails, an error completion is delivered on the receiver on an unspecified execution agent, because there is no agent ofschto deliver it on. - The returned sender is a description only: nothing runs, and
schis not asked to schedule anything, until the sender is connected and started.
Notes
starts_onis not a pipeable adaptor: there is nosndr | starts_on(sch). Its input sender is its second argument, and a pipe would make it read as if it changed where the previous step's result goes, which is what continues_on does.starts_onchanges where the sender that is passed to it is run, not what happens to its result, so it is written as a plain function call, usually at the head of a chain.- The sender returned by
starts_ondoes not report a completion scheduler, because the sender it adapts may complete anywhere. Add continues_on (or use on) if the code that follows needs to run on a known scheduler. - This algorithm was called
onin earlier drafts of the proposal; the nameonnow belongs to the algorithm that also returns to the starting scheduler.
Example
The same work, run on the thread that waits for the result and on a worker thread chosen with starts_on. The continuation after starts_on stays on the worker 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 <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 [a] = std::this_thread::sync_wait(work).value();
auto on_worker = ex::starts_on(worker_loop.get_scheduler(), work);
auto [b] = std::this_thread::sync_wait(on_worker).value();
auto then_where = ex::starts_on(worker_loop.get_scheduler(), work)
| ex::then([&](auto) { return where(); });
auto [c] = std::this_thread::sync_wait(then_where).value();
std::cout << "without starts_on: " << a << '\n'
<< "with starts_on: " << b << '\n'
<< "continuation after it: " << c << '\n';
worker_loop.finish();
}
Output:
without starts_on: main thread
with starts_on: worker thread
continuation after it: worker thread
See also
(C++26) |
adapts a provided sender into one that completes on the provided scheduler's execution resource (customization point object) |
(C++26) |
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) |
| asks a queryable object for the scheduler an operation will be or was started on (customization point object) |