Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

dispatch (4 of 7 overloads)

Submits a function to be run on a specified target executor, and after completion submits the completion handler.

template<
    typename Function,
    typename Executor,
    typename NullaryToken = default_completion_token_t<Executor>>
auto dispatch(
    Function && function,
    const Executor & ex,
    NullaryToken && token = default_completion_token_t< Executor >(),
    constraint_t< is_void< result_of_t< decay_t< Function >()> >::value >  = 0,
    constraint_t<(execution::is_executor< Executor >::value &&can_require< Executor, execution::blocking_t::never_t >::value)||is_executor< Executor >::value >  = 0);

This function submits a function object for execution using the specified executor. The function object may be called from the current thread prior to returning from dispatch(). Otherwise, it is queued for execution. After the submitted function completes, the completion handler is dispatched to run on its associated executor.

Parameters

function

A nullary function to be executed on the target executor.

ex

The target executor.

token

The completion token that will be used to produce a completion handler. The function signature of the completion handler must be:

void handler();
Return Value

This function returns async_initiate<NullaryToken, void()>(Init{ex}, token, forward<Function>(function)), where Init is a function object type defined as:

class Init
{
public:
  using executor_type = Executor;
  explicit Init(const Executor& ex) : ex_(ex) {}
  executor_type get_executor() const noexcept { return ex_; }
  template <typename CompletionHandler>
    void operator()(CompletionHandler&& completion_handler,
      Function&& function) const;
private:
  Executor ex_; // exposition only
};

The function call operator of Init:

Remarks

If the function object throws an exception, that exception is allowed to propagate to the target executor. The behaviour in this case is dependent on the executor. For example, io_context will allow the exception to propagate to the caller that runs the io_context, whereas thread_pool will call std::terminate.

Completion Signature
void()

PrevUpHomeNext