std::execution::get_completion_scheduler
| Defined in header <execution>
|
||
template< class CPO >
struct get_completion_scheduler_t { /* unspecified */ };
|
(1) | (since C++26) |
template< class CPO >
constexpr get_completion_scheduler_t<CPO> get_completion_scheduler{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
template< class Q, class... Env >
/*scheduler*/ get_completion_scheduler<CPO>( const Q& q,
const Env&... env ) noexcept;
|
(3) | (since C++26) |
A query that asks the attributes of a sender, execution::get_env(sndr), for the scheduler on which the sender completes: get_completion_scheduler<execution::set_value_t> asks where it completes with a value, get_completion_scheduler<execution::set_error_t> where it completes with an error, and get_completion_scheduler<execution::set_stopped_t> where it completes with set_stopped. The optional argument env is the environment of the receiver that the sender is going to be connected to, which tells the sender where it is started.
Code that receives a sender can use the answer to run more work where the sender completes, for example with execution::schedule(sch). The standard library uses it in the same way: execution::on(sndr, sch, closure) returns to the scheduler on which sndr completes after running closure on sch, the sender returned by the function of let_value is started on the scheduler on which the input sender completed, and get_completion_domain uses the domain of the completion scheduler when a sender does not report its completion domain directly.
The senders of the standard library answer the query from what they know:
- a sender returned by
execution::schedule(sch)completes onsch; - a sender that completes where it is started, such as the one returned by just or by
execution::scheduleon an inline_scheduler, completes on the scheduler on which it is started, which it can only tell fromenv; - a sender adaptor such as then completes where its child sender completes, as far as it can tell, and
execution::continues_on(sndr, sch)completes onsch; - a sender whose completions can happen on more than one scheduler, such as the sender returned by when_all for senders that complete on different schedulers, does not answer.
get_completion_scheduler<CPO> is ill-formed unless CPO is execution::set_value_t, execution::set_error_t, or execution::set_stopped_t.- the result of
q.query(get_completion_scheduler<CPO>, env...), or ofq.query(get_completion_scheduler<CPO>)if that is not valid, if one of them is valid. If the returned scheduler itself answersget_completion_scheduler<execution::set_value_t>with a different scheduler, that scheduler is asked in turn, until a scheduler answers with itself or does not answer, and the last one is returned: a sender that reports aninline_scheduleras its completion scheduler thus reports the scheduler on which it is started, ifenvtells it; - otherwise, a copy of
q, ifqis a scheduler andenvis not empty.
Parameters
| q | - | the attributes of a sender, or a scheduler |
| env | - | the environment of the receiver that the sender is going to be connected to |
Return value
The scheduler on which the completion selected by CPO happens.
Customization point objects
The name execution::get_completion_scheduler 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::get_completion_scheduler_t. See CustomizationPointObject for details.
Notes
- A sender that answers this query promises where it completes: the behavior is undefined if the sender calls the selected completion function on an execution agent that does not belong to the execution resource of the returned scheduler. The program is ill-formed if a sender answers the query for a completion that it can never perform, according to its completion signatures.
- For the error completions, an adaptor can often not tell where they happen even when it knows where its child sender completes:
execution::then(sndr, f)completes with an error either whensndrdoes, or with the exception thrown byf, which happens on the value completion scheduler ofsndr. - A scheduler can answer the query for its own senders: asking
get_completion_scheduler<CPO>(sch, env...)gives the same result as asking the attributes ofexecution::schedule(sch). If the sender returned byexecution::schedule(sch)reports a value completion scheduler, it compares equal tosch. - Without
env, a sender that completes where it is started cannot tell where it completes, and the query is ill-formed. The algorithms of the standard library always pass the environment of the receiver. - A sender can know its completion domain even when it does not know its completion scheduler: a sender that can complete on one of several schedulers which have the same domain still reports that domain with get_completion_domain.
- Before P3826R5, the query did not accept the environment of the receiver, and the sender returned by
execution::scheduleon any scheduler was required to report that scheduler, so that the senders ofinline_schedulerreportedinline_scheduleritself. In early revisions of the proposal, the query was applied to the sender itself, asget_completion_scheduler<execution::set_value_t>(sndr), and sender algorithms were customized by the completion scheduler of their input sender. Both changes were made before C++26 was published. Older implementations and articles may show the earlier interface.
Example
where asks a sender where it completes with a value. then keeps the completion scheduler of its child, and continues_on replaces it. just cannot tell before it is started, but starts_on tells it.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).
#include <concepts>
#include <execution>
#include <print>
#include <string_view>
namespace ex = std::execution;
// Prints the scheduler on which sndr completes with a value, if sndr can tell
void where(std::string_view what, const ex::sender auto& sndr)
{
constexpr auto get_sch = ex::get_completion_scheduler<ex::set_value_t>;
if constexpr (requires { get_sch(ex::get_env(sndr)); })
{
auto sch = get_sch(ex::get_env(sndr));
bool par = std::same_as<decltype(sch), ex::parallel_scheduler>;
std::println("{}: completes on {}", what,
par ? "the parallel scheduler" : "the run_loop");
}
else
std::println("{}: cannot tell", what);
}
int main()
{
ex::run_loop loop;
auto loop_sch = loop.get_scheduler();
auto par_sch = ex::get_parallel_scheduler();
auto work = ex::schedule(par_sch);
where("schedule(par_sch)", work);
// Adaptors such as then complete where their child completes
auto then_work = work | ex::then([] { return 42; });
where("... | then(f)", then_work);
where("... | then(f) | continues_on(loop_sch)",
then_work | ex::continues_on(loop_sch));
// just completes on whatever scheduler it is started on
where("just(42)", ex::just(42));
where("starts_on(loop_sch, just(42))",
ex::starts_on(loop_sch, ex::just(42)));
// A schedule sender completes on the scheduler that created it
auto sch = ex::get_completion_scheduler<ex::set_value_t>(ex::get_env(work));
std::println("Same scheduler: {}", sch == par_sch);
}
Output:
schedule(par_sch): completes on the parallel scheduler
... | then(f): completes on the parallel scheduler
... | then(f) | continues_on(loop_sch): completes on the run_loop
just(42): cannot tell
starts_on(loop_sch, just(42)): completes on the run_loop
Same scheduler: true
See also
| asks the completion domain associated with a completion tag from a sender's attributes (customization point object) | |
(C++26) |
asks a queryable object for its associated scheduler (customization point object) |
| asks a queryable object for the scheduler an operation will be or was started on (customization point object) | |
(C++26) |
obtains a sender that completes, with no values, on an execution agent of a given scheduler (customization point object) |