std::execution::scheduler
| Defined in header <execution>
|
||
template< class Sch >
concept scheduler =
std::derived_from<
typename std::remove_cvref_t<Sch>::scheduler_concept,
scheduler_tag> &&
/*queryable*/<Sch> &&
requires(Sch&& sch)
{
{ execution::schedule(std::forward<Sch>(sch)) } -> execution::sender;
{ execution::get_forward_progress_guarantee(sch) }
-> std::same_as<execution::forward_progress_guarantee>;
} &&
std::equality_comparable<std::remove_cvref_t<Sch>> &&
std::copyable<std::remove_cvref_t<Sch>>;
|
(1) | (since C++26) |
| Helper tag type |
||
struct scheduler_tag {};
|
(2) | (since C++26) |
scheduler is satisfied by types that are schedulers: lightweight handles to an execution resource, such as a thread pool, an event loop, a GPU stream, or the current thread.scheduler_tag, or a class derived from it, as its member type scheduler_concept.A scheduler is what a program uses to say where work runs. It is a factory of senders: execution::schedule(sch) returns a sender that, when started, completes on an execution agent of the scheduler's execution resource, so that the work attached to it runs there. The sender algorithms that move work from one execution resource to another, such as starts_on, continues_on and on, take a scheduler for that reason. Schedulers are cheap to copy and to compare, and are passed around by value.
A program gets schedulers from execution resources, for example:
run_loop::get_scheduler()from a run_loop, an event loop driven by the thread that calls itsrun;- get_parallel_scheduler, which returns a scheduler for a shared thread pool;
- inline_scheduler, which runs work immediately on the thread that starts it;
- task_scheduler, which can hold any scheduler, hiding its type;
- the environment of a receiver:
read_env(get_scheduler)gives the scheduler suggested by the code that started the work, for example the one that this_thread::sync_wait provides.
Semantic requirements
A type Sch models scheduler only if all of the following hold:
- Copying, assigning, comparing and destroying objects of type
Schdo not throw. These operations, andschedule, can be called from several threads at the same time without data races. - Two schedulers compare equal only if they refer to the same execution resource. Several different schedulers can refer to the same execution resource.
- The sender returned by
schedulecompletes successfully with no values. If it reports a value completion scheduler (get_completion_scheduler<set_value_t>on its attributes), that scheduler compares equal to the one that produced the sender. - Asking a scheduler for its completion scheduler or completion domain gives the same answer as asking the sender returned by its
schedule. - Destroying a scheduler does not wait for the work scheduled through it to complete.
Notes
- A scheduler, the senders obtained from it, and the operations started from them, can only be used while the execution resource exists. For example, they all become invalid when the run_loop they came from is destroyed. To wait until all work that uses an execution resource has finished before destroying the resource, track the work with a counting_scope and join it.
- To write a scheduler, define a type with:
using scheduler_concept = std::execution::scheduler_tag;;- a member function
schedule()that returns a sender that completes withset_value()on the execution resource. The sender should report the scheduler as its value completion scheduler, so that the algorithms that follow it know where they run; - a
noexceptmember functionquery(std::execution::get_forward_progress_guarantee_t)that returns the forward progress guarantee that the agents of the execution resource provide. There is no default: a type without this query is not a scheduler, so that a scheduler never claims a guarantee by accident; operator==, and copy operations that do not throw.
- affine, and therefore task and task_scheduler, only accept schedulers whose schedule sender cannot fail: it completes with
set_value()only, or also withset_stopped()when stop can be requested. scheduler_tagwas calledscheduler_tin earlier drafts.
Example
Demonstrates a simple wrapper for std::execution::run_loop that constantly polls run_loop's queue on a single dedicated 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>
class single_thread_context
{
std::execution::run_loop loop_{};
std::jthread thread_;
public:
single_thread_context()
: thread_([this] { loop_.run(); })
{}
single_thread_context(single_thread_context&&) = delete;
~single_thread_context()
{
loop_.finish();
}
std::execution::scheduler auto get_scheduler() noexcept
{
return loop_.get_scheduler();
}
};
int main()
{
single_thread_context ctx;
std::execution::sender auto snd =
std::execution::schedule(ctx.get_scheduler())
| std::execution::then([]
{
std::cout << "Hello world! Have an int.\n";
return 015;
})
| std::execution::then([](int arg) { return arg + 42; });
auto [i] = std::this_thread::sync_wait(snd).value();
std::cout << "Back in the main thread, result is " << i << '\n';
}
Output:
Hello world! Have an int.
Back in the main thread, result is 55
See also
(C++26) |
obtains a sender that completes, with no values, on an execution agent of a given scheduler (customization point object) |
(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) |
(C++26) |
asks a queryable object for its associated scheduler (customization point object) |