提交一个完成令牌或函数对象以供执行。
template< typename Executor, typename NullaryToken = default_completion_token_t<Executor>> auto dispatch( const Executor & ex, NullaryToken && token = default_completion_token_t< Executor >(), constraint_t< execution::is_executor< Executor >::value||is_executor< Executor >::value > = 0);
此函数提交一个对象,以便使用指定的执行器执行。函数对象可能会在从 dispatch()
返回之前从当前线程调用。否则,它将被排队等待执行。
目标执行器。
将用于生成完成处理程序的完成令牌。完成处理程序的函数签名必须是
void handler();
此函数返回 async_initiate<NullaryToken, void()>(Init{ex}, token)
,其中 Init
是定义为如下的函数对象类型
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) const; private: Executor ex_; // exposition only };
Init:
的函数调用运算符:
通过执行以下操作获取处理程序的关联执行器对象 ex1
,类型为 Ex1
auto ex1 = get_associated_executor(handler, ex);
通过执行以下操作获取处理程序的关联分配器对象 alloc
auto alloc = get_associated_allocator(handler);
如果 execution::is_executor<Ex1>::value
为真,则构造一个函数对象 f
,其成员 executor_
使用 prefer(ex1, execution::outstanding_work.tracked)
初始化,成员 handler_
是 completion_handler
的衰变副本,以及一个执行以下操作的函数调用运算符
auto a = get_associated_allocator(handler_); prefer(executor_, execution::allocator(a)).execute(std::move(handler_));
如果 execution::is_executor<Ex1>::value
为假,则构造一个函数对象 f
,其成员 work_
使用 make_work_guard(ex1)
初始化,成员 handler_
是 completion_handler
的衰变副本,以及一个执行以下操作的函数调用运算符
auto a = get_associated_allocator(handler_); work_.get_executor().dispatch(std::move(handler_), a); work_.reset();
如果 execution::is_executor<Ex>::value
为真,则执行
prefer(ex, execution::allocator(alloc)).execute(std::move(f));
如果 execution::is_executor<Ex>::value
为假,则执行
ex.dispatch(std::move(f), alloc);
void()