boost asio note – Executors
Every asynchronous agent has an associated executor. An agent's executor determines how the agent's completion handlers are queued and ultimately run.
每个异步代理都有一个关联的执行器。 代理的执行者确定代理的完成处理程序如何排队并最终运行
Example uses of executors include:
Coordinating a group of asynchronous agents that operate on shared data structures, ensuring that the agents' completion handlers never run concurrently[5].
Ensuring that agents are run on specified execution resource (e.g. a CPU) that is proximal to data or an event source (e.g. a NIC).
Denoting a group of related agents, and so enabling dynamic thread pools to make smarter scheduling decisions (such as moving the agents between execution resources as a unit).
Queuing all completion handlers to run on a GUI application thread, so that they may safely update user interface elements.
Returning an asynchronous operation's default executor as-is, to run completion handlers as close as possible to the event that triggered the operation's completion.
Adapting an asynchronous operation's default executor, to run code before and after every completion handler, such as logging, user authorisation, or exception handling.
Specifying a priority for an asynchronous agent and its completion handlers.
执行者的示例用途包括:
- 协调一组在共享数据结构上运行的异步代理,确保代理的完成处理程序永远不会同时运行 (boost asio 中称为 strand)。
- 确保代理在最接近数据或事件源(例如 NIC)的指定执行资源(例如 CPU)上运行。
- 表示一组相关的代理,从而使动态线程池能够做出更智能的调度决策(例如将代理作为一个单元在执行资源之间移动)。
- 将所有完成处理程序排队以在 GUI 应用程序线程上运行,以便它们可以安全地更新用户界面元素。
- 按原样返回异步操作的默认执行程序,以尽可能接近触发操作完成的事件运行完成处理程序。
- 调整异步操作的默认执行程序,以在每个完成处理程序之前和之后运行代码,例如日志记录、用户授权或异常处理。
- 指定异步代理及其完成处理程序的优先级。
The asynchronous operations within an asynchronous agent use the agent's associated executor to:
Track the existence of the work that the asynchronous operation represents, while the operation is outstanding.
Enqueue the completion handler for execution on completion of an operation.
Ensure that completion handlers do not run re-entrantly, if doing so might lead to inadvertent recursion and stack overflow.Thus, an asynchronous agent's associated executor represents a policy of how, where, and when the agent should run, specified as a cross-cutting concern to the code that makes up the agent.
异步代理中的异步操作使用代理的关联执行器来:
- 跟踪异步操作所代表的工作是否存在,而操作是未完成的。
- 将完成处理程序加入队列以在操作完成时执行。
- 确保完成处理程序不会重新运行,如果这样做可能会导致无意的递归和堆栈溢出。
因此,异步代理的关联执行器表示代理应该如何、在何处以及何时运行的策略,指定为构成代理的代码的横切关注点。