std::execution::schedule, std::execution::schedule_result_t
| Defined in header <execution>
|
||
struct schedule_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr schedule_t schedule{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto schedule( execution::scheduler auto sch );
|
(3) | (since C++26) |
| Helper result types |
||
template< execution::scheduler Sch >
using schedule_result_t = decltype(schedule(std::declval<Sch>()));
|
(4) | (since C++26) |
Obtains a sender that, when started, schedules work onto the execution resource of the scheduler sch and completes on an execution agent of that resource, without sending any values. This sender is called the schedule sender of sch.
schedule is the basic operation of every scheduler: the scheduler concept is defined by it, and the algorithms that move work to a scheduler, such as starts_on, continues_on and on, are built on it. Calling it directly is the way to begin a chain of work on a given execution resource, such as a thread pool or an event loop: whatever is attached to the returned sender, for example with then, runs on that resource.
schedule_result_t<Sch> is the type of the schedule sender of a scheduler of type Sch.Parameters
| sch | - | the scheduler on whose execution resource the work is to run |
Return value
A sender that, when connected to a receiver and started, asks the execution resource of sch for an execution agent, and completes on that agent:
- on success, with
set_value(), that is, with no values; - if stop has been requested on the receiver's stop token, possibly with
set_stopped()instead, if the scheduler supports cancellation; the schedulers of run_loop, parallel_scheduler and task_scheduler do; - if scheduling fails, possibly with an error, if the scheduler can fail; parallel_scheduler can complete with
set_errorwith a std::exception_ptr.
Which of these can happen is part of the sender's completion signatures, so it is known at compile time.
The returned sender reports sch as its value completion scheduler: if get_completion_scheduler<set_value_t>(get_env(schedule(sch))) is well-formed, it compares equal to sch. Algorithms that follow it in a chain use this to know where they run.
The expression schedule(sch) is expression-equivalent to sch.schedule(). The program is ill-formed if the type of sch.schedule() does not satisfy execution::sender.
Customization point objects
The name execution::schedule 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_t. See CustomizationPointObject for details.
Details
- Nothing is scheduled when the sender is created. Each time the sender is connected to a receiver and started, one new piece of work is scheduled, so a schedule sender can be used in several places, for example several times in a when_all.
- The sender sends no values. To get data into the work, capture it in the function passed to then, or use
starts_on(sch, just(values...))orjust(values...) | continues_on(sch). - Whether a stop request is noticed depends on the scheduler. The scheduler of run_loop checks the receiver's stop token when the scheduled work is about to run, and completes with
set_stopped()instead ofset_value()if stop was requested. If the receiver's stop token can never be stopped (for example, under unstoppable), the schedule senders of run_loop, inline_scheduler and task_scheduler can only complete withset_value(). - A schedule sender, and any operation started from it, is only valid while the execution resource of
schis. Destroying a scheduler does not wait for the work scheduled through it. To wait until that work has finished, for example before destroying the execution resource, start it with spawn in a counting_scope and wait forjoin.
Notes
- Scheduler affinity requires schedulers that cannot fail. affine, which returns to the scheduler an operation was started on, and therefore task and task_scheduler, only accept a scheduler whose schedule sender completes with
set_value()alone, or withset_value()andset_stopped()when stop can be requested. If scheduling could fail with an error or be stopped while returning, the work would complete somewhere other than where it was promised to. - In a task coroutine,
co_await ex::schedule(sch)does not move the rest of the coroutine tosch: every awaited sender is wrapped in affine, so the coroutine resumes on its own scheduler. To run part of the work on another scheduler, awaitex::starts_on(sch, sndr)orex::on(sch, sndr). - inline_scheduler is the exception to the completion scheduler rule: its schedule sender completes immediately on whatever execution agent starts it, so it does not report itself as its completion scheduler. Asked together with the receiver's environment, it reports the receiver's scheduler instead, so the real scheduler is not hidden from the algorithms that follow.
- The completion scheduler of
schedule(sch)also decides which implementation of a following algorithm is used. For example, inschedule(ex::get_parallel_scheduler()) | bulk(...), bulk uses the parallel scheduler's implementation and runs its iterations in parallel. - A scheduler type supports
scheduleby providing a member functionschedule()that returns a sender; see scheduler for the other requirements. A scheduler may also offer other ways of scheduling work that its execution resource supports.
Example
schedule starts work on a worker thread. The same schedule sender is started several times, once per call of sync_wait and three times in when_all, and a stop request made before the work runs turns it into a stopped completion.
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 <stop_token>
#include <thread>
namespace ex = std::execution;
int main()
{
ex::run_loop loop;
std::jthread worker([&] { loop.run(); });
ex::scheduler auto sch = loop.get_scheduler();
// Describes starting work on the worker thread; nothing is scheduled yet.
ex::sender auto begin = ex::schedule(sch);
auto where = [&]
{
return std::this_thread::get_id() == worker.get_id() ? "worker thread"
: "main thread";
};
// The sender sends no values; work attached to it runs where it completes,
// and the sender reports that place as its completion scheduler.
auto [a] = std::this_thread::sync_wait(begin | ex::then(where)).value();
auto cs = ex::get_completion_scheduler<ex::set_value_t>(ex::get_env(begin));
std::cout << std::boolalpha
<< "Work attached with then() ran on the " << a << '\n'
<< "Completion scheduler is sch: " << (cs == sch) << '\n';
// Every time the sender is started, a new piece of work is scheduled.
auto square = [&](int n)
{
return begin | ex::then([n] { return n * n; });
};
auto [x, y, z] = std::this_thread::sync_wait(
ex::when_all(square(1), square(2), square(3))).value();
std::cout << "Squares: " << x << ' ' << y << ' ' << z << '\n';
// If stop is requested before the scheduled work runs, it completes with
// the stopped signal instead, and then() is skipped.
std::inplace_stop_source stop_source;
stop_source.request_stop();
auto job = begin | ex::then([] { std::cout << "Not printed\n"; });
auto stop_env = ex::prop{std::get_stop_token, stop_source.get_token()};
auto cancelled = ex::write_env(std::move(job), stop_env);
auto result = std::this_thread::sync_wait(std::move(cancelled));
std::cout << "Cancelled work sent a value: " << result.has_value() << '\n';
loop.finish();
}
Output:
Work attached with then() ran on the worker thread
Completion scheduler is sch: true
Squares: 1 4 9
Cancelled work sent a value: false
See also
(C++26) |
specifies that a type is a scheduler (concept) |
(C++26) |
adapts a provided sender into one that will start an execution on the provided scheduler's execution resource (customization point object) |
(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) |
| obtains the completion scheduler associated with a completion tag from a sender's attributes (customization point object) |