Namespaces
Variants

std::execution::parallel_scheduler_replacement::query_parallel_scheduler_backend

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
std::shared_ptr<parallel_scheduler_backend> query_parallel_scheduler_backend();
(since C++26)

Returns the backend object through which every parallel_scheduler reaches its execution resource. get_parallel_scheduler calls this function every time it is called, and the scheduler it returns uses the backend object that this function returns.

The standard library provides a definition that returns the backend of the implementation's own execution resource. The function is replaceable: a program can provide its own definition, and then every parallel_scheduler in the program uses the backend that the program's definition returns.

Return value

A std::shared_ptr that points to a parallel_scheduler_backend object, which is a base class subobject of an object of a class derived from it. The object stays alive as long as the returned pointer, or any shared_ptr that shares ownership with it, exists. The pointer does not have to own the object: it can also point to an object with static storage duration that outlives every use of the backend.

Exceptions

Any exception thrown by the definition that the program uses, whether provided by the standard library or by the program. It propagates out of get_parallel_scheduler.

Global replacement

A program replaces the function by defining a function with the same name, parameters and return type in namespace std::execution::parallel_scheduler_replacement, following the rules for replacement functions: for example, the definition must not be inline, and there can be only one in the program. Like the replacements of operator new, the replacement takes effect before the program starts, so it is used by every call, including calls made during the initialization of static objects.

The replacement must return a pointer to a backend object. If it returns a null pointer, get_parallel_scheduler calls std::terminate.

The replacement cannot call the version provided by the standard library, so a program that replaces it cannot use the implementation's execution resource through parallel_scheduler.

Notes

  • Since get_parallel_scheduler calls the function every time, a replacement should normally return the same backend object every time. Schedulers that use different backend objects compare unequal, and work that runs on them does not share an execution resource. The function can be called from several threads at the same time, so it must not cause data races; the initialization of a static local variable is thread-safe.
  • A replacement does not have to keep its backend object alive for the whole program. It can, for example, store a std::weak_ptr to it, and create a new one when the previous one has been destroyed.
  • The namespace parallel_scheduler_replacement was called system_context_replaceability until it was renamed by P4154R0, before C++26 was published. Older implementations and articles may show the earlier name.

Example

A program for a platform without threads replaces the backend with one that runs all work at once, on the thread that starts it. bulk with execution::par still works, but all iterations run on the main thread, in one chunk.
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 <cstddef>
#include <execution>
#include <memory>
#include <print>
#include <span>
#include <thread>

namespace ex = std::execution;
namespace psr = std::execution::parallel_scheduler_replacement;

// A backend for a platform without threads: it runs all work at once,
// on the thread that starts it
struct inline_backend : psr::parallel_scheduler_backend
{
    void schedule(psr::receiver_proxy& r,
                  std::span<std::byte>) noexcept override
    {
        std::println("schedule");
        r.set_value();
    }

    void schedule_bulk_chunked(std::size_t n, psr::bulk_item_receiver_proxy& r,
                               std::span<std::byte>) noexcept override
    {
        std::println("schedule_bulk_chunked({})", n);
        if (n != 0)
            r.execute(0, n);
        r.set_value();
    }

    void schedule_bulk_unchunked(std::size_t n,
                                 psr::bulk_item_receiver_proxy& r,
                                 std::span<std::byte>) noexcept override
    {
        std::println("schedule_bulk_unchunked({})", n);
        for (std::size_t i = 0; i != n; ++i)
            r.execute(i, i + 1);
        r.set_value();
    }
};

// The replacement function: every parallel_scheduler in the program uses
// the backend that it returns
std::shared_ptr<psr::parallel_scheduler_backend>
psr::query_parallel_scheduler_backend()
{
    static auto backend = std::make_shared<inline_backend>();
    return backend;
}

int main()
{
    auto main_id = std::this_thread::get_id();
    auto work = ex::schedule(ex::get_parallel_scheduler())
              | ex::bulk(ex::par, 3, [&](int i)
                {
                    std::println("Iteration {}, on the main thread: {}", i,
                                 std::this_thread::get_id() == main_id);
                });
    std::this_thread::sync_wait(std::move(work));
}

Output:

schedule
schedule_bulk_chunked(3)
Iteration 0, on the main thread: true
Iteration 1, on the main thread: true
Iteration 2, on the main thread: true

See also

obtains a parallel_scheduler for the shared execution resource that runs work in parallel
(function) [edit]
represents a handle of a shared global execution resource (such as a OS-provided system thread pool) optimized for parallel execution
(class) [edit]
serves as the abstraction bridge between the user-facing parallel_scheduler and the backend execution resource (such as a OS-provided system thread pool)
(class) [edit]
allocation functions
(function) [edit]