std::execution::get_forward_progress_guarantee
| Defined in header <execution>
|
||
struct get_forward_progress_guarantee_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr get_forward_progress_guarantee_t
get_forward_progress_guarantee{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
template< class Sch >
execution::forward_progress_guarantee
get_forward_progress_guarantee( const Sch& sch ) noexcept;
|
(3) | (since C++26) |
A query that asks a scheduler what forward progress guarantee the execution agents of its execution resource provide, as a value of forward_progress_guarantee. Code that is given a scheduler uses the answer to decide what the work it schedules there may do: for example, work may wait for other work on the same scheduler only if the answer is forward_progress_guarantee::concurrent.
Unlike most queries, this one is asked of a scheduler, and not of the environment of a receiver. An operation that needs to know whether it may block the execution agent it was started on asks about get_start_scheduler(get_env(rcvr)); one that schedules more work asks about the scheduler it schedules that work on, such as get_scheduler(get_env(rcvr)).
get_forward_progress_guarantee(sch) is expression-equivalent to std::as_const(sch).query(get_forward_progress_guarantee). It is ill-formed if Sch does not satisfy scheduler. The program is ill-formed if the expression std::as_const(sch).query(get_forward_progress_guarantee) can throw an exception, or has a type other than forward_progress_guarantee.Parameters
| sch | - | the scheduler to query |
Return value
The forward progress guarantee of the scheduler. If it is forward_progress_guarantee::concurrent, all execution agents created by the execution resource of sch provide the concurrent forward progress guarantee. If it is forward_progress_guarantee::parallel, they provide at least the parallel forward progress guarantee.
Customization point objects
The name execution::get_forward_progress_guarantee 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_forward_progress_guarantee_t. See CustomizationPointObject for details.
Notes
- A scheduler answers the query with a
constnoexceptmember functionquery(get_forward_progress_guarantee_t)that returns aforward_progress_guarantee. Every scheduler must answer it: theschedulerconcept requiresget_forward_progress_guarantee(sch)to be valid. A scheduler that wraps another scheduler, such as task_scheduler, usually reports the guarantee of the one it wraps. - The answer is a value of the scheduler object, not of its type. It is a constant expression only if the query function of the scheduler is
constexprand does not depend on the state of the object. task_scheduler gives different answers for objects of the same type, so generic code usually checks the answer at run time. - Earlier drafts of C++26 had
get_forward_progress_guarantee(sch)returnforward_progress_guarantee::weakly_parallelfor a scheduler that did not answer the query. LWG issue 4354 removed this default before C++26 was published. Some implementations still have it.
Example
here_scheduler runs work on the execution agent that starts it, and reports weakly_parallel. The scheduler of run_loop reports parallel in the reference implementation; the standard does not specify its answer yet (LWG issue 4635).
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 <execution>
#include <print>
#include <string_view>
#include <thread>
#include <utility>
namespace ex = std::execution;
// Runs work on the execution agent that starts it, so it cannot promise
// more than that agent provides: weakly parallel forward progress
struct here_scheduler
{
using scheduler_concept = ex::scheduler_tag;
template<class Rcvr>
struct operation
{
using operation_state_concept = ex::operation_state_tag;
Rcvr rcvr;
void start() & noexcept { ex::set_value(std::move(rcvr)); }
};
struct sender
{
using sender_concept = ex::sender_tag;
template<class Self, class... Env>
static consteval auto get_completion_signatures()
{
return ex::completion_signatures<ex::set_value_t()>();
}
template<class Rcvr>
operation<Rcvr> connect(Rcvr rcvr) const { return {std::move(rcvr)}; }
};
sender schedule() const noexcept { return {}; }
// The answer to the query
constexpr ex::forward_progress_guarantee
query(ex::get_forward_progress_guarantee_t) const noexcept
{
return ex::forward_progress_guarantee::weakly_parallel;
}
bool operator==(const here_scheduler&) const = default;
};
std::string_view name(ex::forward_progress_guarantee guarantee)
{
switch (guarantee)
{
case ex::forward_progress_guarantee::concurrent:
return "concurrent";
case ex::forward_progress_guarantee::parallel:
return "parallel";
case ex::forward_progress_guarantee::weakly_parallel:
return "weakly_parallel";
}
return "unknown";
}
int main()
{
// The query function of here_scheduler is constexpr
static_assert(ex::get_forward_progress_guarantee(here_scheduler{}) ==
ex::forward_progress_guarantee::weakly_parallel);
ex::run_loop loop;
std::jthread worker = std::jthread([&] { loop.run(); });
auto loop_sch = loop.get_scheduler();
std::println("Run loop: {}",
name(ex::get_forward_progress_guarantee(loop_sch)));
// task_scheduler reports the guarantee of the scheduler it holds, which
// is known only at run time
for (ex::task_scheduler sch : {ex::task_scheduler(here_scheduler{}),
ex::task_scheduler(loop_sch)})
std::println("Task scheduler: {}",
name(ex::get_forward_progress_guarantee(sch)));
// A sender can ask about the scheduler it was started on
auto [guarantee] = std::this_thread::sync_wait(
ex::starts_on(loop_sch, ex::read_env(ex::get_start_scheduler))
| ex::then([](auto sch)
{
return ex::get_forward_progress_guarantee(sch);
})).value();
std::println("Under starts_on: {}", name(guarantee));
loop.finish();
}
Output:
Run loop: parallel
Task scheduler: weakly_parallel
Task scheduler: parallel
Under starts_on: parallel
See also
| specifies a forward progress guarantee of execution agents created by the scheduler's associated execution resource (enum) | |
(C++26) |
specifies that a type is a scheduler, a handle to an execution resource (concept) |
(C++26) |
asks a queryable object for its associated scheduler (customization point object) |
| asks a queryable object for a scheduler that can be used to delegate work to for the purpose of forward progress delegation (customization point object) |