stim::executor_interface struct

Interface for enabling multi-threading.

Example

Given a hypothetical thread_pool type:

class thread_pool
{
  public:
    template <typename Func>
    void enqueue(Func&&) noexcept;

    template <typename T, typename Func>
    void for_each(T&& collection, Func&&) noexcept;

    size_t worker_count() noexcept;
};

A fully-conforming implementation would be:

#include <stim/stim.h>

class thread_pool_executor final : public stim::executor_interface
{
  private:
    thread_pool pool{ /* ... */ };

  public:
    void enqueue(stim::const_span<stim::executor_task> tasks) noexcept override
    {
        if (tasks.size() > 1u)
        {
            pool.for_each(tasks, [](auto& task) noexcept { invoke(task); });
        }
        else
        {
            pool.enqueue([task = tasks[0]]() noexcept { invoke(task); });
        }
    }

    size_t concurrency() const noexcept override
    {
        return pool.worker_count();
    }
};

If your local thread pool does not have a for_each (or equivalent) it is sufficient to simply enqueue the tasks individually, e.g.:

void enqueue(stim::const_span<stim::executor_task> tasks) noexcept override
{
    for (auto& task : tasks)
        pool.enqueue([=]() noexcept { invoke(task); });
}

Public static variables

static size_t max_concurrency constexpr
The absolute maximum batch size the library will wish to subdivide a task into.

Public static functions

static void invoke(const executor_task& task) noexcept
Invokes an executor task.

Public functions

auto concurrency() const -> size_t pure virtual noexcept
Returns the maximum number of invocations the library may subdivide a task into.
void enqueue(const_span<executor_task> tasks) pure virtual noexcept
Enqueues one or more tasks for invocation.

Function documentation

static void stim::executor_interface::invoke(const executor_task& task) noexcept

Invokes an executor task.

size_t stim::executor_interface::concurrency() const pure virtual noexcept

Returns the maximum number of invocations the library may subdivide a task into.

void stim::executor_interface::enqueue(const_span<executor_task> tasks) pure virtual noexcept

Enqueues one or more tasks for invocation.

Parameters
tasks The tasks being enqueued.

Variable documentation

static size_t stim::executor_interface::max_concurrency constexpr

The absolute maximum batch size the library will wish to subdivide a task into.