Namespaces
Variants

std::execution::get_parallel_scheduler

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
execution::parallel_scheduler get_parallel_scheduler();
(since C++26)

Returns a parallel_scheduler for the shared execution resource that runs work in parallel. This function is the only way to create a parallel_scheduler other than by copying one.

The function obtains the backend object through which the scheduler reaches the execution resource, by calling parallel_scheduler_replacement::query_parallel_scheduler_backend() (see query_parallel_scheduler_backend). If it returns a null pointer, std::terminate is called. Otherwise, the returned scheduler uses the backend object that the pointer points to.

Return value

A parallel_scheduler that uses the backend object returned by query_parallel_scheduler_backend.

Exceptions

Any exception thrown by query_parallel_scheduler_backend.

Notes

  • The function calls query_parallel_scheduler_backend on every call. The implementation's version of that function usually returns the same backend object every time, so all the schedulers in the program compare equal and share one execution resource. The standard does not require this: an implementation can create the backend again, for example after the previous one was destroyed, and a replacement provided by the program can return different backend objects from different calls. Schedulers that use different backend objects compare unequal.
  • A parallel_scheduler keeps its backend object alive: the std::shared_ptr returned by query_parallel_scheduler_backend owns it, and the scheduler and its copies share that ownership.
  • The standard does not restrict when the function can be called. It can be called before main starts and after it returns, for example in the constructors and destructors of static objects.
  • The function was proposed as get_system_scheduler. Earlier revisions of the proposal had a class system_context, whose objects stood for the execution resource and provided the scheduler. Older implementations and articles may show these names.

Example

Two parts of a program that know nothing about each other run their work on the parallel scheduler, without passing schedulers or thread pools between them.
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 <thread>
#include <utility>

namespace ex = std::execution;

// Two parts of a program that know nothing about each other. Instead of
// creating thread pools of their own, both run their work on the parallel
// scheduler, which shares one execution resource between them
namespace physics
{
    ex::sender auto step(int n)
    {
        return ex::schedule(ex::get_parallel_scheduler())
             | ex::then([n]
               {
                   return std::pair(n * 2, std::this_thread::get_id());
               });
    }
}

namespace audio
{
    ex::sender auto mix(int n)
    {
        return ex::schedule(ex::get_parallel_scheduler())
             | ex::then([n]
               {
                   return std::pair(n + 1, std::this_thread::get_id());
               });
    }
}

int main()
{
    auto [p, a] = std::this_thread::sync_wait(
        ex::when_all(physics::step(20), audio::mix(41))).value();

    auto main_id = std::this_thread::get_id();
    std::println("physics: {}, on the main thread: {}",
                 p.first, p.second == main_id);
    std::println("audio: {}, on the main thread: {}",
                 a.first, a.second == main_id);

    // Every call returns a handle to the same execution resource
    std::println("Same execution resource: {}",
                 ex::get_parallel_scheduler() == ex::get_parallel_scheduler());
}

Output:

physics: 40, on the main thread: false
audio: 42, on the main thread: false
Same execution resource: true

See also

represents a handle of a shared global execution resource (such as a OS-provided system thread pool) optimized for parallel execution
(class) [edit]
returns the backend object that every parallel_scheduler uses; a program can replace it to provide its own backend
(function) [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]
obtains a sender that completes, with no values, on an execution agent of a given scheduler
(customization point object)[edit]