Namespaces
Variants

std::execution::forward_progress_guarantee

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
enum class forward_progress_guarantee
{
    concurrent,
    parallel,
    weakly_parallel
};
(since C++26)

forward_progress_guarantee specifies the forward progress guarantee provided by execution agents created by the execution resource associated with a scheduler.

A scheduler can report this value through std::execution::get_forward_progress_guarantee.

Every scheduler reports a value: the concept requires get_forward_progress_guarantee(sch) to be valid and to have the type forward_progress_guarantee. A scheduler type provides it with a const noexcept member function query(get_forward_progress_guarantee_t) that returns the guarantee. Code that is given a scheduler can use this query to find out whether it is safe to schedule work that waits for other work.

Member constants

Name Meaning
concurrent Execution agents provide the concurrent forward progress guarantee: each agent eventually makes progress in a finite amount of time, independently of whether other agents make progress.
parallel Execution agents provide at least the parallel forward progress guarantee: an agent is not required to make progress before it has started executing, but after it has executed a step, it provides the concurrent forward progress guarantee.
weakly_parallel Execution agents provide only the weakly parallel forward progress guarantee: an agent is not guaranteed to eventually make progress, although its guarantee can be strengthened by forward progress delegation.

Notes

  • The guarantee tells what work scheduled on a scheduler may wait for:
  • With concurrent, work may block until other work on the same scheduler does something, as threads may. A scheduler that runs each piece of work on a new thread can provide it.
  • With parallel, work that has started keeps running, but work that has not started yet may have to wait until other work finishes. This is the guarantee of a thread pool with a queue: if all threads of the pool wait for work that is still in the queue, nothing makes progress. Work on such a scheduler should not wait for other work scheduled on it.
  • With weakly_parallel, even work that has started is not guaranteed to make progress, for example on hardware that runs groups of agents in lockstep, or when work runs on whatever agent starts it. Such work should not block at all.
  • parallel_scheduler reports parallel, and task_scheduler reports the guarantee of the scheduler it wraps. The guarantee of the scheduler of run_loop is not specified, and inline_scheduler does not provide the query, so it does not satisfy scheduler (LWG issue 4635); the proposed resolution makes them report parallel and weakly_parallel, which is what the reference implementation reports.
  • A scheduler should report the strongest guarantee that it is sure to provide, and no stronger. If it reports a weaker guarantee than it provides, code that checks the guarantee may choose a slower strategy. If it reports a stronger one, code that relies on the guarantee may block forever.
  • Earlier drafts of C++26 had get_forward_progress_guarantee(sch) return weakly_parallel for a scheduler that does not provide the query. LWG issue 4354 removed this default before C++26 was published, because a scheduler author could easily forget the query and report a weaker guarantee than the scheduler provides. Some implementations still have the default.

Example

exchange schedules a consumer that waits for a value and a producer that provides it, but only if the scheduler guarantees that the producer makes progress while the consumer waits. new_thread_scheduler is a scheduler that runs each piece of work on a new thread, and reports concurrent. The scheduler of a run_loop that is run by one thread does not provide this guarantee.
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 <atomic>
#include <exception>
#include <execution>
#include <print>
#include <string_view>
#include <thread>
#include <utility>

namespace ex = std::execution;

// Runs each piece of work on a new thread. Threads provide concurrent
// forward progress, so this scheduler reports it
struct new_thread_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
        {
            try
            {
                auto run = [this] { ex::set_value(std::move(rcvr)); };
                std::thread(run).detach();
            }
            catch (...)
            {
                ex::set_error(std::move(rcvr), std::current_exception());
            }
        }
    };

    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(),
                ex::set_error_t(std::exception_ptr)>();
        }

        template<class Rcvr>
        operation<Rcvr> connect(Rcvr rcvr) const { return {std::move(rcvr)}; }
    };

    sender schedule() const noexcept { return {}; }

    ex::forward_progress_guarantee
    query(ex::get_forward_progress_guarantee_t) const noexcept
    {
        return ex::forward_progress_guarantee::concurrent;
    }

    bool operator==(const new_thread_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";
}

// The consumer waits for a value from the producer, which is scheduled at
// the same time. This works only if the producer makes progress while the
// consumer waits, which requires concurrent forward progress
void exchange(ex::scheduler auto sch)
{
    auto guarantee = ex::get_forward_progress_guarantee(sch);
    std::print("{}: ", name(guarantee));
    if (guarantee != ex::forward_progress_guarantee::concurrent)
    {
        std::println("waiting for other work could block forever");
        return;
    }

    std::atomic<int> box = 0;
    auto consumer = ex::schedule(sch) | ex::then([&]
    {
        box.wait(0);
        return box.load();
    });
    auto producer = ex::schedule(sch) | ex::then([&]
    {
        box = 42;
        box.notify_one();
    });
    auto [value] = std::this_thread::sync_wait(
        ex::when_all(consumer, producer)).value();
    std::println("received {}", value);
}

int main()
{
    exchange(new_thread_scheduler{});

    // One thread runs the work of the loop, one piece at a time. If the
    // consumer ran first, the producer would never run
    ex::run_loop loop;
    std::jthread worker = std::jthread([&] { loop.run(); });
    exchange(loop.get_scheduler());
    loop.finish();
}

Output:

concurrent: received 42
parallel: waiting for other work could block forever

See also

asks a scheduler about its execution::forward_progress_guarantee
(customization point object)[edit]
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)[edit]
specifies that a type is a scheduler, a handle to an execution resource
(concept) [edit]
represents a handle of a shared global execution resource (such as a OS-provided system thread pool) optimized for parallel execution
(class) [edit]